Unix Network Programming Assignment-1
Winter Semester, Computer Science Department.
University of Hyderabad.
                                              DeadLine : 27-01-05.

1.Which system call is used to implement native Linux Threads?
2.Can the priority inversion problem happen with user-level threads? why?
why not?
3.In a system with threads, is there one stack per thread or one stack per
process when user-level threads are used? What about when kernel-level
threads are used? Explain.
4.Two computer science students, Siva and SreeKanth, are having discussion
about the following program thread1.c .  Siva says that the output of the
program is as shown in thread1.out. Sreekanth disagrees.  Who is correct?
What modification to the program is required so that it gives an output as
shown in  thread1.out? Write your programs to produce the required output.
thread1.c :
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS    8

char *messages[NUM_THREADS];

void *PrintHello(void *threadid)
{
   int *id_ptr, taskid;

   sleep(3);
   id_ptr = (int *) threadid;
   taskid = *id_ptr;
   printf("Thread %d: %s \n", taskid, messages[taskid]);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc, t;

   messages[0] = "Hi";
   messages[1] = "Enjoy";
   messages[2] = "The";
   messages[3] = "Unix";
   messages[4] = "Networkprogramming";
   messages[5] = "Course";
   messages[6] = "With";
   messages[7] = "GNU/Linux";

   for(t=0;t<NUM_THREADS;t++)
   {
      printf("Creating thread %d\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);
      if (rc)
      {
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

thread1.out:

Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
Creating thread 5
Creating thread 6
Creating thread 7
Thread 0: Hi
Thread 1: Enjoy
Thread 2: The
Thread 3: Unix
Thread 4: NetworkProgramming
Thread 5: Course
Thread 6: With
Thread 7: GNU/Linux