You are on page 1of 5

Operating system labwork

Pablo Jimnez Mateo

1.As we can see in the first output it only prints numbers in and increment of three:

If we put a simple printf telling us the value of nextp it shows us that the producer generates more data than the consumer is able to consume, overwriting some values in the buffer:

So a semaphore implementation as seen in the book would work:

2.In this exercise the critical section is protected using a mutex (Mutual exclusion variable). Basically every thread races to lock the mutex. Only one thread can lock it, everyone else will have to wait. The owner of the mutex performs his operations and then unlocks the mutex. Again the rest of the threads race for the mutex and this process is repeated. critsem.c has two tasks. The first one generates the alphabet swapping one letter to the left in every iteration. Task 2 produces sequences from 1 to 9 of 26 digits and also swaps one number to the left. The program creates three threads; one for Task 1, one for Task 2 and one for the screen, assigns each function to each thread and makes them wait until everyone else has finished. 3.It can be implemented using a variable where the turn will be stored and other where it tells whether or not a process wants to access to his critical section.

4.- There is clearly a deadlock in the program I have, the output is:

The resource allocation graph would be:

5.The method used in dead2 to avoid deadlocks is a method based in the order of the resources. A process that holds a resource Ri can only request a resource Rj iif Rj>Ri, Being Rk an arbitrary order. It is explained in further detail in the page 216 of the book.

6.For this exercise the global variable THEIRPORT was 6900 and MYPORT was 4000. a) I used the line printf(My mother is: %d\n,getppid()); to check it and indeed, the child knows his mothers PID:

And it is also obvious that the mother knows her childs PID b) Looking at the code I see that it needs the address, the port, the data and the length of the data. c) The function fork() returns a 0 to the children, so in the line 30 of fork_socket.c only the chid executes the execl(). d) Line 68 of the fork_socket.c program should be changed, instead of 127.0.0.1 the receivers IP should be written. e) As soon as I could understand what did the htons(int) function I found that this line would print the port: printf(My mother port is: %d\n, ntohs(their_addr.sin_port));

You might also like