ProvaPratica 2010.07.19

From Sistemi Operativi
Revision as of 08:48, 9 May 2017 by FedericoB (talk | contribs) (Aggiunta soluzione di un'altro studente e migliorata organizzazione pagina.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Link to exam

Esercizio1

Soluzione di Gab

#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

Soluzione di Gab

#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;
}

Esercizio 3

Esame 19 07 10

Lo script deve prendere in input un file e invertire le righe  
pari con quelle dispari. 
Se l'input e'
hello
world
goodbye
moon
l'output deve essere
world
hello
moon
goodbye

Soluzione di Pierg

import sys

lines = []
arg = sys.argv[1]
with open(arg,'r') as objectarg:
  for line in objectarg:  
    lines.append(line)
    if 'str' in line:
      break

print (lines)

i = 0
j = 1
 
while (j < len(lines)):
	temp = lines[i]
	lines[i] = lines[j]
	lines[j] = temp
	i += 2
	j += 2

print (lines)