Anda di halaman 1dari 3

....

void *inc_x(void *x_void_ptr)


{
int *x ptr = (int *x)x_void_ptr;
for (int i = 0; i <COUNT; i++)
*x_ptr++;
return NULL;
}

int main()
{
static int x = 0;
pthread_t (THREADS);
printf("Initial value of x);

for(int i = 0; i<THREADS: i++)


{
if (pthread_create()
{
...
}
}...
}...
//Create thread and then wait for it to end before creating a new one
[HE IS GOING TO POST THIS PROGRAM ON BLACKBOARD]
*Won't be in quiz 1 since it's in the class before the quiz

--------

Thread Synchronization is necessary

User Level Threads: All thread managment is done by the application. The kernel is
not aware of the existence of threads
Advantage of ULTs: Thread switching does not require kernel, so thread switching
occurs in the threads library / userspace - where there is a scheduler
*Thread switching does not require kernel mode privileges - no context switching
*Scheduling cna be application specific
*ULTs can run on any OS - it's a library so you can use it anywhere
Disadvantage of ULTs
*In a typical OS system calls are blocking
-Ways to overcome, Jacketing: Converts a blocking system call to a non-blocking
system call

Kernel-Level Threads (KLTs)


Thread management is done by the kernel, i.e, Windows
Advantages: Kernel can schedule multiple threads for the same process on multiple
processor, if one thread in a process is blocked the kernel can schedule another
thread of the same process,
and kernel routines can be multithreaded
Disadvantage: Transfer of control from one thread to another within the same
process requires a mode switching
Combined Approaches
*Thread creation done in userspace
*Bulk of scheduling and synchronization done by kernel
*Example is Solaris

Applications That Benefit


*Multithreaded native applications
*Multiprocess applications
*Java applications
*Multiinstance applications
+Threads are in the same address space but processes are not, threads are
lightweight processes

------

NEXT CLASS: Interprocess Communication


*After the quiz will be a practical section

Message Passing
*Processes that want to exchange data send and recieved messages
*Any message exchange requires
-One send: send(addr, msg, length);
-One recieve: recieve(...);

Advantages
*Very general - sender and recieves can be on different machines
*Relatively secure - reciever can inspect messages it has recieved

Disadvantages
*Hard to use
-Every data transfer requires a send() and a recieve()
-Recieving process must expect the send()

Shared Memory
Two or more processes share a part of their address space

Advantages
Fast and easy to use

Disadvantage
Sender and recievers must be on same machine and less secure

*The quiz is next thursday, so next Tuesday is next Tuesday


Quiz is over Unit 1 [No threads]