Starting from:

$35

CSCI 4210 - Exam 2 Solved

 — Operating Systems † 




 1. (6 POINTS) When you call pthread_join(), what happens? Indicate the best answer.

(a)  The thread terminates and returns a specified value

(b)  The child thread that calls pthread_join() is acknowledged as being terminated

(c)  The thread disconnects from its parent thread

(d)  The thread detaches by terminating and returning NULL

(e)  A child thread is joined back into the corresponding parent thread

(f)  The process exits via the exit() system call

2. (6 POINTS) Consider a mutex declared as type p_thread_mutex_t() and shared memory being used by multiple fork() processes. Whichof the following are true? Indicate the best answer.

(a)mutex cannot be used with shared memory

(b)mutex can be used with shared memory with the correct attributes set

(c)mutex is not needed because shared memory automatically enforces mutual exclusion

(d)Items a & c

(e)Items b & c

(f)None of the above

 3. (6 POINTS) The code below has memory leaks. How many total bytes are leaked when you run it. Indicate the best answer.

1 void * q3( void * y )

2 {

3    int * x = (void *)y;

4    char * s = calloc( *x + 16, sizeof( char ) );

5   sprintf( s, "ABCDEFGH" );

6   fprintf( stderr, "%s", s );

7 return NULL;

8 }

9 int main()

10 {

11    int * z = calloc( 1, sizeof( int ) );

12    if (fork() == 0) *z = 10;

13 pthread_t t1;

14 pthread_create( &t1, NULL, q3, z );

15   fprintf( stderr, "ERROR" );

16   pthread_join( t1, NULL );

17 return EXIT_SUCCESS;

18 }

(a) 50                     (c) 4                         (e) 20

(b) 32                     (d) 46                        (f) 16

4. (6 POINTS) When you call shmget(), what happens? Indicate the best answer.

(a)  The process is attached to the given shared memory ID, if valid

(b)  Given a shared memory key, the shared memory ID is determined (and returned)

(c)  A shared memory segment is removed and its data is destroyed.

(d)  The process or thread detaches from the given shared memory ID

(e)  Given a shared memory ID, the size of the shared memory segment is returned

(f)  None of the above

 5. (6 POINTS) Assume that initially there is no shared memory segment with key 4000. If you run the given code below three times, what is the terminal output of the last program execution and how many times is the shared memory segment removed? Indicate the best answer.

1 int main( int argc, char * argv[] )

2 {

3 int shmkey = 4000;

4    int shmid = shmget( shmkey, sizeof( int ), IPC_CREAT | 0660 );

5 int * data = shmat( shmid, NULL, 0 );

6 int flush=0;

7 pid_t pid = fork();

8    if ( pid > 0 ) waitpid( pid, NULL, 0 );

9    int i, stop = 4;

10    for ( i = 1 ; i < stop ; i++ ) *data += i;

11     if ( pid == 0 ) printf( "Sum is %d\n", *data );

12    if (*data > 31) flush = 1;

13    shmdt( data );

14    if (flush) shmctl( shmid, IPC_RMID, 0 );

15 return EXIT_SUCCESS;

16 }

(a)      "Sum is 6" shared memory removed 2 times.

(b)      "Sum is 18" shared memory removed 0 times.

(c)      "Sum is 30" shared memory removed 1 times.

(d)      "Sum is 42" shared memory removed 0 times.

(e)      "Sum is 6" shared memory removed 1 times.

(f)      "Sum is 18" shared memory removed 1 times.

6. (6 POINTS) For a UDP socket, what does a return value of 0 indicate in the recvfrom() system call? Indicate the best answer.

(a)  No space is available to store the received data (i.e., we avoid bu↵er overflow)

(b)  An error occurred

(c)  No data (i.e., zero bytes) were received from the remote end

(d)  The remote end closed its socket

(e)  For UDP-based communication, recvfrom() will not return 0

(f)   None of the above

 For Questions 7 and 8, use the code shown below.

1 unsigned long x = 5;

2 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

3 #define CHILDREN 8

4 void * q7_8( void * arg )

5 {

6    for (int ctr=1; ctr<=10; ctr++)

7 {

8     x++;

9 }

10 pthread_exit( NULL );

11 }

12 int main()

13 {

14 pthread_t tid[CHILDREN];

15   int i;

16    for ( i = 0 ; i < CHILDREN ; i++ )

17   {

18      int * t = malloc( sizeof( int ) );

19      *t = 2 + i * 2;

20      pthread_create( &tid[i], NULL, q7_8, NULL );

21   }

22    for ( i = 0 ; i < CHILDREN ; i++ )

23 {

24      pthread_join( tid[i], NULL );

25 }

26    printf("x is %ld\n", x);

27 return EXIT_SUCCESS;

28 }

7. (6 POINTS) What output CANNOT occur? Indicate the best answer.

(a) 67                                    (d) 87

(b) 85                                    (e) 12

(c) 35                                    (f) All outputs are possible.

8. (6 POINTS) Where should pthread_mutex_lock() and pthread_mutex_unlock() be placed to protect the critical section. Indicate the best answer.

(a) Before 16 and after 21              (d) Before and after 8

(b) Before 22 and after 25              (e) Before and after 26

(c) Before and after 24                  (f) There is no critical section

 9. (6 POINTS) Consider the pseudocode for the Producer/Consumer problem. What code should be inserted ar Position 1 and Position 2 to make this pseudocode correct? Indicate the best answer.

int n = 20;

buffer_t buffer[n];

semaphore empty_slots = n; semaphore used_slots = 0;

/* producer */                             /* consumer */

while ( 1 )                                       while ( 1 )

{                                                 {

item = produce_next_item();                P( used_slots );

P( empty_slots );                            item = remove_from_buffer();

add_to_buffer( item );                   /* Position 2 */

/* Position 1 */                            consume( item );

}                                                 }

(a)   Position 1: V( empty slots ); Position 2: V( used slots );

(b)   Position 1: P( used slots ); Position 2: P( empty slots );

(c)   Position 1: V( used slots ); Position 2: V( empty slots );

(d)   Position 1: P( empty slots );, Position 2: P( used slots );

(e)   No answer is correct

(f)    All answers will work correctly

10. (6 POINTS) What distinguishes a binary semaphore from a counting semaphore? Indicate the best answer.

(a)    Binary semaphores can only be implemented with mutexes, counting semaphores cannot have mutexes

(b)    Counting semaphores can only be used for process synchronization, not data synchro­nization

(c)     Binary semaphores provide unique (single process) access to a resource or critical section

(d)    Only counting semaphores define the V() operation.

(e)    All of the above

(f)      None of the above

 11. (6 POINTS) Assume that the server program below has just started running and has received the following 4 datagrams: ”I never eat cereal, but I know a professor who does!\n”, ”Ping\n”, ”Ping\n”, ”Ping\n”. Receiving which of the following messages would result in a the server sending a return datagram?

Note that in UDP, when a datagram is received, if the given bu
↵er is not large enough, 
datagrams are truncated (i.e., bytes in the datagram beyond the size of the bu↵er are ignored).

Indicate the best answer.

#define MAXBUFFER 8

int main()

{

int flag = 0;

char buffer[ MAXBUFFER + 1 ];

int sd = socket( AF_INET, SOCK_DGRAM, 0 );

struct sockaddr_in server; server.sin_family = AF_INET;

server.sin_addr.s_addr = htonl( INADDR_ANY );

server.sin_port = htons( 8128 );

bind( sd, (struct sockaddr *) &server, sizeof( server ) );

struct sockaddr_in client;

int n, len = sizeof( client ); while ( 1 )

{

n = recvfrom( sd, buffer, MAXBUFFER, 0, (struct sockaddr *) &client, 
(socklen_t *) &len );

if ( n > 0 )

{

flag = (flag + n) % 17; if (flag == 7 )

{

sendto( sd, "Okay, enough!", 13, 0, (struct sockaddr *) &client, len );

}

}

else return EXIT_FAILURE;

(a)  "\n"

(b)  "g\n"

(c)  "ng\n"

(d)  "ing\n"

(e)  "Ping\n"

(f)  None of the above

 12. (6 POINTS) How many distinct values could variable x have at the end of the main() function in the code below? Indicate the best answer.

void * q12( void * arg )

{

int * s = (int *)arg;

*s += 3;

return NULL;

}

int main()

{

pthread_t tid1, tid2;

int x = 5;

pthread_create( &tid1, NULL, q12, &x ); pthread_create( &tid2, NULL, q12, &x ); x = 9;

pthread_join( tid1, NULL );

pthread_join( tid2, NULL );

/* what could variable x be here? */

return EXIT_SUCCESS;

}

(a) Only one possible value

(b) Exactly two possible values

(c) Exactly three possible values

(d) Exactly four possible values

(e) Exactly five possible values

(f)  Exactly six possible values

 13. (12 POINTS) What is the exact terminal output of the code below? If multiple outputs are possible, succinctly describe all possibilities. You may use a diagram or enumerate the outcomes.

void * q13( void * arg )

{

int * s = (int *)arg;

*s += 3;

return NULL;

}

int main()

{

int *x = malloc(sizeof(int));

*x = 5;

int pid1, pid2;

pid1 = fork();

*x = 7;

if (pid1 != 0) pid2 = fork();

if (pid1 == 0)

{

q13(x);

} else if (pid2 == 0) {

q13(x);

} else {

*x += 9;

}

printf("x = %d\n", *x);

free(x);

int status;

if (pid1 != 0 && pid2 != 0)

{

waitpid(pid1, &status, 0);

waitpid(pid2, &status, 0);

}

return EXIT_SUCCESS;

}

Write your answer to this question on the answer sheet or type it into your text file for submission via Submitty.

 14. (16 POINTS) What is the exact terminal output of the code below? If multiple outputs are possible, succinctly describe all possibilities. You may use a diagram or enumerate the outcomes.

void * q14( void * args )

{

int t = *(int *)args;

char buffer[32];

int rc = read( t, buffer, 9 );

buffer[rc] = '\0';

printf( "%s", buffer );

return NULL;

}

int main()

{

char * s = "CUCKOO FOR COCOA PUFFS";

int p[2];

close( 2 );

close( 0 );

pipe( p );

write( p[1], s, 11 ); pid_t pid = fork(); pthread_t t1, t2, t3, t4;

if ( pid == 0 )

{

fprintf( stderr, "%s", s );

printf( "I'M " );

pthread_create( &t1,
NULL,
q14,
&p[0]
);
pthread_create( &t2,
NULL,
q14,
&p[0]
);
pthread_join( t1, NULL ); pthread_join( t2, NULL );

}

else /* pid > 0 */

{

wait( NULL );
 
 
 
 
pthread_create( &t3,
NULL,
q14,
&p[0]
);
pthread_create( &t4,
NULL,
q14,
&p[0]
);
pthread_join( t3, NULL ); pthread_join( t4, NULL );

}

return EXIT_SUCCESS;

}

Write your answer to this question on the answer sheet or type it into your text file for submission via Submitty.

More products