ProvaPratica 2010.07.19

From Sistemi Operativi
Revision as of 22:46, 1 May 2014 by Gab (talk | contribs) (Created page with "[http://www.cs.unibo.it/~renzo/so/pratiche/2010-07-19.pdf Link to exam] Esercizio1 <syntaxhighlight lang="C"> #include "stdlib.h" #include "stdio.h" #include "unistd.h" #inc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Link to exam

Esercizio1

#include "stdlib.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include <sys/time.h>
#include <signal.h>

#define MESSAGE_LENGTH 40
#define NUMBER_OF_MESSAGE 100000


int main(int argc,char *arv[])
{
	int childToParentPipe[2];
	int parentToChildPipe[2];
	pipe(childToParentPipe);
	pipe(parentToChildPipe);
	int childPID = fork();
	if(childPID)
	{
		/* parent */
		int toReadDescriptor = childToParentPipe[0];
		int toWriteDescriptor = parentToChildPipe[1];
		close(childToParentPipe[1]);
		close(parentToChildPipe[0]);
		char messageToSend[MESSAGE_LENGTH];
		int index = 0;
		for(index = 0 ; index<MESSAGE_LENGTH;index++)
			messageToSend[index] = 'M';
		messageToSend[MESSAGE_LENGTH-1]='\0';
		char messageToRead[MESSAGE_LENGTH];
		struct timeval startTime;
		struct timeval endTime;
		gettimeofday(&startTime,NULL);
		for(index = 0;index <NUMBER_OF_MESSAGE;index++ )
		{
			write(toWriteDescriptor,messageToSend,MESSAGE_LENGTH);
			read(toReadDescriptor,&messageToRead,MESSAGE_LENGTH);
		}
		gettimeofday(&endTime,NULL);
		struct timeval timeUsed;
		timersub(&endTime,&startTime,&timeUsed);
		printf("Time taken %ld seconds and %d useconds \n", timeUsed.tv_sec, timeUsed.tv_usec);
		kill(childPID,SIGTERM);
	}
	else
	{
		/* child */
		int toReadDescriptor = parentToChildPipe[0];
		int toWriteDescriptor = childToParentPipe[1];
		close(childToParentPipe[0]);
		close(parentToChildPipe[1]);
		char arrivedMessage[MESSAGE_LENGTH];
		while(1)
		{
			read(toReadDescriptor,arrivedMessage,MESSAGE_LENGTH);
			write(toWriteDescriptor,arrivedMessage,strlen(arrivedMessage));
		}
	}
	return 0;
}


Esercizio2

#include "stdlib.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include <sys/time.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>

#define MESSAGE_LENGTH 40
#define NUMBER_OF_MESSAGE 100000

#define PARENT_TO_CHILD_TAG 1

#define CHILD_TO_PARENT_TAG 2


typedef struct MessageBody
{
	long type;
	char message[MESSAGE_LENGTH];
} MessageBody;

int main(int argc,char *arv[])
{
	int messageQueueID = msgget(IPC_PRIVATE, 0666);
	int childPID = fork();
	if(childPID)
	{
		/* parent */
		MessageBody messageToSend;
		messageToSend.type = 1;
		int index;
		for(index = 0;index<MESSAGE_LENGTH;index++)
			messageToSend.message[index] = 'M';
		messageToSend.message[MESSAGE_LENGTH-1] = '\0';
		MessageBody messageReceived;
		struct timeval startTime;
		struct timeval endTime;
		gettimeofday(&startTime,NULL);
		for(index = 0;index < NUMBER_OF_MESSAGE;index++)
		{
			msgsnd(messageQueueID,&messageToSend,MESSAGE_LENGTH,0);
			msgrcv(messageQueueID,&messageReceived,MESSAGE_LENGTH,2,MSG_NOERROR);
		}
		gettimeofday(&endTime,NULL);
		struct timeval timeUsed;
		timersub(&endTime,&startTime,&timeUsed);
		printf("Time taken %ld seconds and %d useconds \n", timeUsed.tv_sec, timeUsed.tv_usec);
		kill(childPID,SIGTERM);
	}
	else
	{
		/* child */
		while(1)
		{
			MessageBody messageReceived;
			msgrcv(messageQueueID,&messageReceived,MESSAGE_LENGTH,1,MSG_NOERROR);
			messageReceived.type = 2;
			msgsnd(messageQueueID,&messageReceived,MESSAGE_LENGTH,0);
		}
	}
	return 0;
}

Gab