0% found this document useful (0 votes)
24 views

Lecture Notes 13

Uploaded by

Sayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture Notes 13

Uploaded by

Sayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

C/C++ Programming in a UNIX Environment

CS 3377

Bhanu Kapoor, PhD


Department of Computer Science
University of Texas, Dallas, TX
[email protected]

1
Reading for Week 11 and 12
 Chapter 8 of Advacned Programming in
the UNIX Environment Stevens and
Rago. (Week 11) Process Control
 Chapter 11 of Advacned Programming in
the UNIX Environment Stevens and
Rago. (Week 12) Threads

2
Agenda
 UNIX Process Creation and Management
 Threads: Pthreads

3
CPU Process Switching by OS

4
Silberschatz, Galvin, and Gagne©1999
Process-based Operating System

 Most of OS executes within user


processes
 Uses two categories of processes
◼ System processes
◼ User processes

5
Unix Process State Transition Diagram

Sleep = Blocked
6
UNIX Process Creation
 Every process, except process 0, is created by
the fork() system call
◼ fork() allocates entry in process table and assigns a
unique PID to the child process
◼ child gets a copy of process image of parent: both
child and parent share the same code following
fork(), different data
◼ but fork() returns the PID of the child to the parent
process and returns 0 to the child process
◼ Optional Exec() system call can be used after a
fork to replace the process’ memory space with a
new program

7
UNIX Process Creation
 Parent process can wait() for
completion of child
◼ The child process can pass data back to
parent via exit() call, picked up by parent
via the wait().
◼ Terminated child is a “zombie” if parent
does not wait() for it

8
UNIX System Processes

 “Boot” loads the kernel image

 Process 0 is created at boot time and


becomes the “swapper” after forking
process 1 (the INIT process)

 When a user logs in: process 1 creates


a process for that user

9
Unix Tree of Processes

Silberschatz, Galvin, and Gagne©1999


10
Unix Subprocesses: System calls
 #include <unistd.h>
 pid_t fork()
◼ Creates new process image which is an
(almost) exact copy of the one that invokes it
 int execv(char*filename,char* argv[])
 int execl(char*filename,char* arg0,
char* arg1,… NULL)
◼ Replace current process image with one
running the named program

11
Wait functions
 #include <sys/wait.h>
 pid_t waitpid(pid_t pid, int* status_ptr,
int options)
◼ Waits for completion of a particular child
process
 pid_t wait(int* status_ptr)
◼ Waits for any one child to terminate
 pid_t getpid(void)
◼ Returns process ID
 pid_t getppid(void) (parent ID)

12
Processes on typical Solaris
Process Creation

 Assign a unique process identifier


(PID).
 Allocate space for the process image.
 Initialize process control block
◼ many default values (e.g., state is New, no
I/O devices or files...).
 Set up appropriate linkages
◼ Ex: add new process to linked list used for
the scheduling queue.
Process Creation
 Address space
◼ Child duplicate of parent.
◼ Child has a program loaded into it.
 UNIX examples
◼ fork system call creates new process.
◼ exec system call used after
a fork to replace the
process’ memory space
with a new program.
Process Creation
C Program Forking
int main() {
pid_t pid;
/* fork another process */
pid = fork(); //there are two processes now
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process , child process executes here*/
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process, parent process executes here */
/* parent will wait for the child to complete */
wait (NULL); /* NULL returned by the child process in the end */
printf ("Child Complete");
exit(0);
}
}
Shared Memory Architecture

Copyright © 2010,
Elsevier Inc. All rights
Reserved
Processes and Threads
 A process is an instance of a running (or
suspended) program.
 Threads are analogous to a “light-
weight” process.
 In a shared memory program a single
process may have multiple threads of
control.

Copyright © 2010,
Elsevier Inc. All rights
Reserved
Logical View of Threads
 Threads are created within a process

A process Process hierarchy


T2
T4
T1
shared code, data
P1
and kernel context
sh sh sh
T5 T3
foo
Concurrent Thread Execution
 Two threads run concurrently if their logical
flows overlap in time
 Otherwise, they are sequential (we’ll see that
processes have a similar rule)
 Examples:
◼ Concurrent:
Thread A Thread B Thread C
A & B, A&C
◼ Sequential:
B&C
Time
Threads
 “A thread is a basic unit of CPU utilization;”
◼ CPU is dispatched to execute threads.
◼ Threads are scheduled for CPU cycles.
◼ Thread within the same process share a
code section, data section, and other
resources such as open files and signals.
(signals discussed later).
 Idea behind threads is that the process can
perform many different activities at the same
time (concurrently, of course, on a single CPU)

22
Threads
 Processes with a single thread of control can
perform one task at a time
 Management of multiple threads of control
essential now for concurrent execution of tasks
 Most modern desktop and notebook computers
have multiple cores
 What is multiple threads of control? →

23
Multiple Threads of Control
 Most modern applications are developed as a
separate process with several threads of control.
◼ Number of separate ‘tasks’ that are part of
the application can be executed concurrent
with other tasks.
◼ Example: Word processor
 Echoing keystrokes
 Undertaking styling formatting
 Checking and doing automatic correction
◼ If these tasks had to be executed
sequentially, performance would be
unbearable or simply would not be done thus
reducing the overall quality of, say, a
document.
Multiple Threads of Control
 Process Creation:
◼ Process creation is time consuming
◼ Way of doing business before we used threads.
 Multiple threads for doing disjoint tasks concurrently
 Solution is to have the server create a single thread that
‘listens’ for new requests for service.
 When received, server creates a thread to service the
request.
 Examples:
◼ RPCs are multi-threaded
◼ Kernels are multi-threaded
Single and Multithreaded Processes

It is important to note, however, that a multi-threaded process


shares resources with its siblings and parent process.
Multithreading Benefits
 Responsiveness
 Resource Sharing
 Economy – since threads share resources, it is
easier to context-switch threads than context-
switching processes.
 Utilization of MP Architectures – significant
increases in performance in a multiprocessor
system, where different threads may be running
simultaneously (in parallel) on multiple
processors.
 Of course, there’s never ‘a free lunch,’ as we will
see later. (There’s always a cost…; nothing this
good comes free. ☺ )
Multicore Programming

Challenges for Multicore Programming:


• Dividing Activities
• Balance
• Data Splitting
• Data Dependency
• Test and Debug

28
Multithreading
 Performing multiple threads of execution in
parallel
◼ Replicate registers, PC, etc.
◼ Fast switching between threads
 Fine-grain multithreading
◼ Switch threads after each cycle
◼ Interleave instruction execution
◼ If one thread stalls, others are executed
 Coarse-grain multithreading
◼ Only switch on long stall (e.g., L2-cache miss)
◼ Simplifies hardware, but doesn’t hide short stalls (eg,
data hazards)

29
Simultaneous Multithreading
 In multiple-issue dynamically scheduled
processor
◼ Schedule instructions from multiple threads
◼ Instructions from independent threads execute when
function units are available
◼ Within threads, dependencies handled by scheduling
and register renaming
 Example: Intel Pentium-4 HT
◼ Two threads: duplicated registers, shared function
units and caches

30
Multithreading Example

31
Shared Memory Programming

Several Thread Libraries/systems


 Pthreads is the POSIX Standard
◼ Relatively low level
◼ Portable but possibly slow; relatively heavyweight
 OpenMP standard for application level programming
◼ Support for scientific programming on shared memory
◼ http://www.openMP.org
 Java Threads
 TBB: Thread Building Blocks
◼ Intel
 CILK: Language of the C “ilk”
◼ Lightweight threads embedded into C

32
Overview of POSIX Threads
 POSIX: Portable Operating System Interface
for UNIX
◼ Interface to Operating System utilities
 PThreads: The POSIX threading interface
◼ System calls to create and synchronize threads
◼ In CSIL, compile a c program with gcc -lpthread
 PThreads contain support for
◼ Creating parallelism and synchronization
◼ No explicit support for communication, because
shared memory is implicit; a pointer to shared data is
passed to a thread

33
Unix Processes vs. Pthreads
C function for starting a thread
pthread.h
One object for
each thread.
pthread_t

int pthread_create (
pthread_t* thread_p /* out */ ,
const pthread_attr_t* attr_p /*
in */ ,
void* (*start_routine ) ( void ) /*
in */ ,
void* arg_p /* in */ ) ; Copyright © 2010,
Elsevier Inc. All rights
Reserved
A closer look (1)

int pthread_create (
pthread_t* thread_p /* out */ ,
const pthread_attr_t* attr_p /*
in */ ,
void* (*start_routine ) ( void ) /*
in */ ,
void* arg_p /* in */ ) ;
We won’t be using, so we just pass NULL.

Allocate before calling.

Copyright © 2010,
Elsevier Inc. All rights
Reserved
A closer look (2)

int pthread_create (
pthread_t* thread_p /* out */ ,
const pthread_attr_t* attr_p /*
in */ ,
void* (*start_routine ) ( void ) /*
in */ ,
void* arg_p /* in */ ) ;
Pointer to the argument that should
be passed to the function start_routine.
The function that the thread is to run.

Copyright © 2010,
Elsevier Inc. All rights
Reserved
pthread_create
 Prototype:
void* thread_function ( void* args_p ) ;
 Void* can be cast to any pointer type in C.
 So args_p can point to a list containing one
or more values needed by thread_function.
 Similarly, the return value of thread_function
can point to a list of one or more values.

Copyright © 2010,
Elsevier Inc. All rights
Reserved
Wait for Completion of Threads
pthread_join(pthread_t *thread, void
**result);
◼ Wait for specified thread to finish. Place exit
value into *result.
 We call the function pthread_join once
for each thread.
 A single call to pthread_join will wait for
the thread associated with the pthread_t
object to complete.

Copyright © 2010,
Elsevier Inc. All rights
Reserved
Example of Pthreads
#include <pthread.h>
#include <stdio.h>
void *PrintHello(void * id){
printf(“Thread%d: Hello World!\n", id);
}

void main (){


pthread_t thread0, thread1;
pthread_create(&thread0, NULL, PrintHello, (void *) 0);
pthread_create(&thread1, NULL, PrintHello, (void *) 1);
}
Example of Pthreads with join
#include <pthread.h>
#include <stdio.h>
void *PrintHello(void * id){
printf(“Hello from thread %d\n", id);
}

void main (){


pthread_t thread0, thread1;
pthread_create(&thread0, NULL, PrintHello, (void *) 0);
pthread_create(&thread1, NULL, PrintHello, (void *) 1);
pthread_join(thread0, NULL);
pthread_join(thread1, NULL);
}
Some More Pthread Functions
 pthread_yield();
◼ Informs the scheduler that the thread is willing to
yield
 pthread_exit(void *value);
◼ Exit thread and pass value to joining thread (if exists)
 pthread_t me; me = pthread_self();
◼ Allows a pthread to obtain its own identifier pthread_t
thread;
 Synchronizing access to shared variables
◼ pthread_mutex_init, pthread_mutex_[un]lock
◼ pthread_cond_init, pthread_cond_[timed]wait
Pthreads: POSIX Threads
 Pthreads is a standard set of C library functions
for multithreaded programming
◼ IEEE Portable Operating System Interface, POSIX,
section 1003.1 standard, 1995
 Pthread Library (60+ functions)
◼ Thread management: create, exit, detach, join, . . .
◼ Thread cancellation
◼ Mutex locks: init, destroy, lock, unlock, . . .
◼ Condition variables: init, destroy, wait, timed wait
 Programs must include the file pthread.h
 Programs must be linked with the pthread
library (-lpthread)

43
The Pthreads API
 Thread management: The first class of
functions work directly on threads - creating,
terminating, joining, etc.
 Semaphores: provide for create, destroy,
wait, and post on semaphores.
 Mutexes: provide for creating, destroying,
locking and unlocking mutexes.
 Condition variables: include functions to
create, destroy, wait and signal based upon
specified variable values.

44
Thread Creation
pthread_create (tid, attr, start_routine, arg)
 It returns the new thread ID via the tid
argument.
 The attr parameter is used to set thread
attributes, NULL for the default values.
 The start_routine is the C routine that the thread
will execute once it is created.
 A single argument may be passed to
start_routine via arg. It must be passed by
reference as a pointer cast of type void.

45
Thread Termination and Join
pthread_exit (value) ;
 This Function is used by a thread to
terminate. The return value is passed as a
pointer.

pthread_join (tid, value_ptr);


 The pthread_join() subroutine blocks the
calling thread until the specified threadid
thread terminates.
 Return 0 on success, and negative on
failure. The returned value is a pointer
returned by reference. If you do not care
about the return value, you can pass NULL for
the second argument.
46
#include <pthread.h>
#include <stdio.h> Cast (void *) to integer
#include <stdlib.h>
(since void * is a pointer
#define NUM_THREADS 5 and pointer is nothing
other than 32 bit integer
void *HelloWorld(void *threadid)
{ value.)
int tid;
tid = (int)threadid;
Start of thread
printf("Hello World! It's me, thread #%d!\n", tid);
pthread_exit(0); execution
}

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


Compile using gcc –lpthread
{
pthread_t threads[NUM_THREADS];

int i;
Thread
handles
for(i=0;i<NUM_THREADS;i++)
{ Handle of the created
pthread_create(&threads[i], 0, HelloWorld, thread
(void *)i);
}

pthread_exit(0);
return 0;
} Address of
Argument to the function 47
start function
Simple pthread hello
#include <pthread.h>
#include <stdio.h>
#define THREADS 4
/* the function called for each thread */
void* f(void* data) {
printf("Hello from a thread!\n");
pthread_exit(NULL);
}
int main ( ) {
/* an array of threads */
pthread_t threads[THREADS];
/* start all of the threads */
int t;
for (t = 0; t < THREADS; t++) {
pthread_create(&threads[t], NULL, f, NULL);
}
pthread_exit(NULL);
} 48
Pthread hello with thread id arg

49
Example Compiling a Pthread program

gcc −g −Wall −o pth_hello pth_hello.c


−lpthread

link in the Pthreads library

Copyright © 2010,
Elsevier Inc. All rights
Reserved

You might also like