SlideShare a Scribd company logo
Process Management
By
Utkarsh Kulshrestha
1.Process Concept:
2.Operations On Process:
3.Inter Process Communication:
Contents
OS Performs following Actions While
creating Process
Assign Process Id and Priority
Allocating Memory and other
resources
Set up the Process environment
Creating PCB
Return Process Id
Process States
Process ID
Priority
Process State
PSW
CPU register
Event Information
Memory Allocation
Resource Held
PCB Pointer
Process
Scheduling
Information
PSW & CPU reg
information
Info for which
process is waiting
Pointer to another
PCB
Process Control Block(PCB)
Interaction Of Processes
Parent process create children processes, which, in turn create other
processes, forming a tree of processes.
 Resource sharing
✦ Parent and children share all resources.
✦ Children share subset of parent’s resources.
✦ Child will not exceed resource limits on that process.
 Execution
✦ Parent and children execute concurrently.
✦ Parent waits until children terminate.
Process Creation
✦ 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 (Cont.)
The system calls used for the creation of the process:
1.A call to fork() will create a completely separate sub-
process which will be exactly the same as the parent.
2.The process that initiates the call to fork is called the
parent process.
3.The new process created by fork is called the child
process.
4.The child gets a copy of the parent's text and memory
space.
5.They do not share the same memory .
Fork()
fork() system call returns an integer to both the parent
and child processes:
-1 this indicates an error with no child process
created.
A value of zero indicates that the child process code is
being executed.
Positive integers represent the child’s process
identifier (PID) and the code being executed is in the
parent’s process.
Fork(contd.)
if ( (pid = fork()) == 0)
printf(“I am the childn”);
else
printf(“I am the parentn”);
Simple Fork Example
Calling one of the exec() family will terminate the currently
running program and starts executing a new one which is
specified in the parameters of exec in the context of the
existing process. The process id is not changed.
exec()
• int execl( const char *path, const char *arg, ...);
• int execlp( const char *file, const char *arg, ...);
• int execle( const char *path, const char *arg , ..., char * const envp[]);
• int execv( const char *path, char *const argv[]);
• int execvp( const char *file, char *const argv[]);
Exec family of functions
Inter-Process Communication Mechanisms
Purposes for IPC
UNIX Pipes
• Pipe sets up communication channel between two
(related) processes.
Two processes connected by a pipe
• One process writes to the pipe, the other reads from the
pipe.
• Looks exactly the same as reading from/to a file.
• System call:
int fd[2] ;
pipe(&fd[0]) ;
fd[0] now holds descriptor to read from pipe
fd[1] now holds descriptor to write into pipe
UNIX Pipes
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
char *message = "This is a message!!!" ;
main()
{ char buf[1024] ;
int fd[2];
pipe(fd); /*create pipe*/
if (fork() != 0) { /* I am the parent */
write(fd[1], message, strlen (message) + 1) ;
}
else { /*Child code */
read(fd[0], buf, 1024) ;
printf("Got this from MaMa!!: %sn", buf) ;
}
}
UNIX Pipes
Shared Memory
Common chunk of read/write memory
among processes
Proc. 1 Proc. 2
ptr
Attach
Proc. 3 Proc. 4 Proc. 5
ptr ptr ptr
ptr
Attach
Create
Shared Memory
(unique key)
0
MAX
Creating Shared Memory
int shmget(key_t key, size_t size, int shmflg);
Example:
key_t key;
int shmid;
key = ftok(“<somefile>", ‘A');
shmid = shmget(key, 1024, 0644 | IPC_CREAT);
Here’s an example: shm_create.c.
Attach and Detach
Shared Memory
void *shmat(int shmid, void *shmaddr, int shmflg);
int shmdt(void *shmaddr);
Example:
key_t key;
int shmid;
char *data;
key = ftok("<somefile>", ‘A');
shmid = shmget(key, 1024, 0644);
data = shmat(shmid, (void *)0, 0);
shmdt(data);
Here’s an shm_attach.c
Deleting Shared Memory
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmctl(shmid, IPC_RMID, NULL);
Example: Shm_delete.c
FIFOs (named pipes)
FIFOs are sometimes called named pipes. Pipes can be
used only between related processes when a common
ancestor has created the pipe. With FIFOs, unrelated
processes can exchange data.
Creating a FIFO is similar to creating a file.
#include <sys/ stat.h>
int mkfifo( const char *pathname, mode_t mode);
Returns: 0 if OK,-1 on error.
Opening a FIFO
● open(pathname, O_RDONLY)
● Open read end
● open(pathname, O_WRONLY)
● Open write end
● open() locks until other end is opened
● Opens are synchronized
open(pathname, O_RDONLY |
O_NONBLOCK) can be useful
Message Queues
● Message-oriented communication
● Receiver reads messages one at a time
– No partial or multiple message reads
● Unlike pipes, multiple readers/writers can be useful
● Messages have priorities
● Delivered in priority order
● Message notification feature
Message queue operations
Int msgget(key_t, int flag)
Int msgctl(int msgid, int cmd, struct msgid_ds *buf)
Int msgsnd(int msgid, const void *ptr, size nbytes, int flag);
Int msgrcv(int msgid, void *ptr, size_t nbytes, long type, int flag);
Semaphores
• A semaphore is a counter used to provide access to a shared
data object for multiple processes.
• To obtain a shared resource, a process needs to do the
following:
1.Test the semaphore that controls the resource.
1. If the value of the semaphore is positive, the process can use
the resource. In this case, the process decrements the
semaphore value by 1, indicating that it has used one unit of
the resource.
3.If the value of the semaphore is 0, the process goes to sleep
until the semaphore value is greater than 0. When the
process wakes up, it returns to step 1.
• The first function to call is semget to obtain a semaphore ID.
#include <sys/ sem.h>
int semget(key_t key, int nsems, int flag);
Returns: semaphore ID if OK, -1 on error.
• The number of semaphores in the set is nsems. If a new set is
being created (typically in the server), we must specify
nsems. If we are referencing an existing set (a client), we
can specify nsems as 0.
semget()
Gets a semaphore set. The value returned is its id, for use
with other calls.
int semget(key_t key, int n, int flags);
key is the key associated with the semaphore set you want.
Don’t think about it—just use IPC PRIVATE. n is the number of
semaphores the set should contain. flags specifies how how
the set should be allocated. SHM R | SHM W is the best thing
to pass.
semop()
Performs a semaphore operation (i.e. incrementing, decrementing,
etc.) on the selected members of a semaphore set. This is one of
those ones that should really be a bunch of seperate calls.
int semop(int id, struct sembuf* op, unsigned n);
id is the semaphore set’s id. op is the operation to perform. n is
the number of semaphores to affect. You’ll nearly always be
passing in a value of 1 here. struct sembuf’s sem op field is
important. It specifies what you want to do to the semaphore, be
it incrementing, decrementing, or toasting over an open fire8 . • A
non-zero value will be added to the semaphore’s value. Note that
this means negative values indicate subtraction. • A value of zero
will make the operation block until the semaphore value becomes
zero.
semctl()
A bit like shmctl(), but for semaphores. Again, ridiculously
overcomplicated.
int semctl(int id, int iSem, int cmd, union semun arg);
id is the semaphore set id. iSem is the semaphore you want
to twiddle. Only valid with some of the commands. cmd is the
command you want to perform. arg is used for fiddling with
semaphore values. With everything but cmd set to SETALL,
just pass NULL. There are two values for cmd worth looking
at: SETALL and IPC RMID. For details on the others, type man
semctl.
Signals
Signals
• A signal is a mechanism for notifying a program that
some event has occurred.
• intuition: signal  “software interrupt”
• when a signal is sent to a program, its normal execution is
interrupted
• depending on (1) the state of the program, and (2) the type of
signal, the program may
• enter some pre-specified signal handler; or
• take some default action.
Example Signals (not a complete list)
Signal Name Number Description
SIGHUP 1 Hangup (POSIX)
SIGINT 2 Terminal interrupt (ANSI)
SIGQUIT 3 Terminal quit (POSIX)
SIGILL 4 Illegal instruction (ANSI)
SIGTRAP 5 Trace trap (POSIX)
SIGFPE 8 Floating point exception (ANSI)
SIGKILL 9 Kill(can't be caught or ignored) (POSIX)
SIGSEGV 11 Invalid memory segment access (ANSI)
SIGTERM 15 Termination (ANSI)
SIGSTKFLT 16 Stack fault
SIGSTOP 19 Stop executing(can't be caught or ignored) (POSIX)
SIGPROF 27 Profiling alarm clock (4.2 BSD)
SIGWINCH 28 Window size change (4.3 BSD, Sun)
SIGPWR 30 Power failure restart (System V)
… … …
Signal Sources
a process
window
manager
shell command
kernel
SIGWINCH
SIGKILL
SIGINT SIGHUP
SIGQUIT
SIGALRM
SIGPIPE
SIGUSR1
Synchronous vs. Asynchronous Signals
• Synchronous signals:
• arise from executing an instruction in the process’s instruction
stream
• e.g.: illegal instruction (SIGILL); illegal address (SIGSEGV)
• causes a trap into the OS kernel trap handler
• sometimes referred to as “traps”
• directed to the process/thread that executed the instruction
• Asynchronous signals:
• source is external to the current execution
• e.g.: profiling clock (SIGPROF); terminal interrupt, ^C (SIGINT)
Signal Handling
• Use the signal handling library: signal.h
• Then can use the signal call:
#include <signal.h>
void (*signal( int sig, void (*handler)(int))) (int) ;
• signal returns a pointer to the PREVIOUS signal
handler
• #include <signal.h>
typedef void Sigfunc(int); /* my defn */
Sigfunc *signal( int signo, Sigfunc *handler );
Signal is a function
that takes two
arguments:
sig and handler
The signal to be
caught or ignored
is given as argument
sig
The function to be called
when the specified signal
is received is given as a
pointer to the function
handler
The handler function
Receives a single integer
Argument and returns void
The signal function itself
returns a pointer to a function.
The return type is the same
as the function that is passed in,
i.e., a function that takes an
int and returns a void
The returned function
takes a integer
parameter.
Specifying a Signal Handler
Specifying a Signal Handler
Behind the scenes of a SIGSEGV
• When a program tries to access a bad address:
• execution traps into the OS kernel
• if no handler is specified:
• kernel invokes the default handler
• default handler prints out “Segmentation fault” and kills the
process
• if a handler is specified:
• kernel executes the handler
• the expectation is that the handler fixes the problem
• restarts the offending operation
• this allows programmer-controlled recovery from errors
Sending signals
• A program can send a signal to another program using
the kill() system call:
int kill(pid_t pid, int sig)
sends the signal number sig to process pid
(see /usr/include/asm-generic/signal.h)
• A user can send a signal from the command line using
the kill command:
kill –N pid
E.g., “kill -9 pid” (9 = SIGKILL)
Sending Signals via Function Call
raise()
int raise(int iSig);
• Commands OS to send a signal of type iSig to current process
• Returns 0 to indicate success, non-0 to indicate failure
Example
int ret = raise(SIGINT); /* Process commits suicide. */
assert(ret != 0); /* Shouldn't get here. */
Sending Signals via Function Call
kill()
int kill(pid_t iPid, int iSig);
• Sends a iSig signal to the process whose id is iPid
• Equivalent to raise(iSig) when iPid is the id of current process
• Editorial comment: Better function name would be sendsig()
Example
pid_t iPid = getpid(); /* Process gets its id.*/
kill(iPid, SIGINT); /* Process sends itself a
SIGINT signal (commits
suicide?) */
• Suspend the calling process until a signal is
caught.
• #include <unistd.h>
int pause(void);
• Returns -1 with errno assigned EINTR.
(Linux assigns it ERESTARTNOHAND).
• pause() only returns after a signal handler has
returned.
pause()
alarm()
• Set an alarm timer that will ‘ring’ after a specified
number of seconds
• a SIGALRM signal is generated
• #include <unistd.h>
long alarm(long secs);
• Returns 0 or number of seconds until previously set
alarm would have ‘rung’.
Installing a Signal Handler
signal()
sighandler_t signal(int iSig,
sighandler_t pfHandler);
• Installs function pfHandler as the handler for signals
of type iSig
• pfHandler is a function pointer:
typedef void (*sighandler_t)(int);
• Returns the old handler on success, SIG_ERR on error
• After call, pfHandler is invoked whenever process
receives a signal of type iSig
Installing a Handler Example 2
Program testsignalall.c:
#define _GNU_SOURCE
#include <stdio.h>
#include <assert.h>
#include <signal.h>
static void myHandler(int iSig) {
printf("In myHandler with argument %dn", iSig);
}
…
Installing a Handler Example 2 (cont.)
Program testsignalall.c (cont.):
…
int main(void) {
void (*pfRet)(int);
pfRet = signal(SIGHUP, myHandler); /* 1 */
pfRet = signal(SIGINT, myHandler); /* 2 */
pfRet = signal(SIGQUIT, myHandler); /* 3 */
pfRet = signal(SIGILL, myHandler); /* 4 */
pfRet = signal(SIGTRAP, myHandler); /* 5 */
pfRet = signal(SIGABRT, myHandler); /* 6 */
pfRet = signal(SIGBUS, myHandler); /* 7 */
pfRet = signal(SIGFPE, myHandler); /* 8 */
pfRet = signal(SIGKILL, myHandler); /* 9 */
…
Installing a Handler Example 2 (cont.)
Program testsignalall.c (cont.):
…
/* Etc., for every signal. */
printf("Entering an infinite loopn");
for (;;)
;
return 0;
}
Process management

More Related Content

PPT
Microkernel Development
PPT
Евгений Крутько, Многопоточные вычисления, современный подход.
PDF
Mc Squared
PDF
Windbg랑 친해지기
PDF
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
PDF
Joel Falcou, Boost.SIMD
PDF
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
PDF
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
Microkernel Development
Евгений Крутько, Многопоточные вычисления, современный подход.
Mc Squared
Windbg랑 친해지기
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Joel Falcou, Boost.SIMD
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321

What's hot (20)

PDF
asyncio internals
PPTX
Node.js System: The Landing
PDF
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
PDF
Python Async IO Horizon
PDF
第二回CTF勉強会資料
PPTX
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
PDF
Metaprogramming and Reflection in Common Lisp
PPTX
Grand Central Dispatch in Objective-C
PDF
Qt Rest Server
PPT
PDF
Multithreading done right
PDF
TensorFlow XLA RPC
PDF
Ownership System in Rust
PDF
Devirtualizing FinSpy
PDF
Protocol handler in Gecko
PPTX
NSOperation objective-c
PDF
Virtual Machine Constructions for Dummies
PDF
Exploitation of counter overflows in the Linux kernel
PPTX
Дмитрий Демчук. Кроссплатформенный краш-репорт
asyncio internals
Node.js System: The Landing
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
Python Async IO Horizon
第二回CTF勉強会資料
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Metaprogramming and Reflection in Common Lisp
Grand Central Dispatch in Objective-C
Qt Rest Server
Multithreading done right
TensorFlow XLA RPC
Ownership System in Rust
Devirtualizing FinSpy
Protocol handler in Gecko
NSOperation objective-c
Virtual Machine Constructions for Dummies
Exploitation of counter overflows in the Linux kernel
Дмитрий Демчук. Кроссплатформенный краш-репорт
Ad

Viewers also liked (20)

PDF
Приоритеты высшего образования в контексте развития информационно-коммуникаци...
PPT
2010 Strategic Plan Roll out 6-24-10 (FINAL)
PDF
Mobile note madvertise/ Bemobee oct_2016_fr
PDF
PowerPoint - Current-3
PDF
Re-Implementing Apache Thrift using Model-Driven Engineering Technologies: An...
PPTX
Push plateforme bewoopi
PDF
IstoÉ João Cabral
PPTX
Application métier Gabon mining
DOCX
Modulo de young
DOC
Holy sutura.vol.1.jpg.doc
PDF
Android 6 marshmallow
PPTX
EPSILON AUTO PVT LTD Company Profile Kitchen New 240315
PDF
IDEALIN FOGGING SYSTEMS : FOGMAX Humidifier
PPTX
Moving to Continuous Delivery with XebiaLabs XL Release
PPTX
Primera semana diapositivas-2016-i
PPT
Nem adiantou
PDF
Boss guide effects
PDF
Conflitos Familiares
PPTX
проект огород на окне
PPT
Revolución China 1949 power point
Приоритеты высшего образования в контексте развития информационно-коммуникаци...
2010 Strategic Plan Roll out 6-24-10 (FINAL)
Mobile note madvertise/ Bemobee oct_2016_fr
PowerPoint - Current-3
Re-Implementing Apache Thrift using Model-Driven Engineering Technologies: An...
Push plateforme bewoopi
IstoÉ João Cabral
Application métier Gabon mining
Modulo de young
Holy sutura.vol.1.jpg.doc
Android 6 marshmallow
EPSILON AUTO PVT LTD Company Profile Kitchen New 240315
IDEALIN FOGGING SYSTEMS : FOGMAX Humidifier
Moving to Continuous Delivery with XebiaLabs XL Release
Primera semana diapositivas-2016-i
Nem adiantou
Boss guide effects
Conflitos Familiares
проект огород на окне
Revolución China 1949 power point
Ad

Similar to Process management (20)

PPTX
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
DOC
computer notes - Inter process communication
PPTX
Linux Systems Programming: Process CommunCh11 Processes and Signals
PDF
MultiThreading-in-system-and-android-logcat-42-.pdf
PPT
What is-a-computer-process-os
PDF
fg.workshop: Software vulnerability
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
PDF
[HITB Malaysia 2011] Exploit Automation
PDF
Skiron - Experiments in CPU Design in D
PDF
[Kiwicon 2011] Post Memory Corruption Memory Analysis
PDF
Os lab final
PDF
Alexander Reelsen - Seccomp for Developers
PPT
[CCC-28c3] Post Memory Corruption Memory Analysis
PDF
System Calls - Introduction
DOCX
Program Assignment Process ManagementObjective This program a.docx
PPT
process creation OS
PDF
OSTEP Chapter2 Introduction
PDF
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
PPTX
04_ForkPipe.pptx
PDF
Unix presentation.pdf
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
computer notes - Inter process communication
Linux Systems Programming: Process CommunCh11 Processes and Signals
MultiThreading-in-system-and-android-logcat-42-.pdf
What is-a-computer-process-os
fg.workshop: Software vulnerability
Beyond Breakpoints: A Tour of Dynamic Analysis
[HITB Malaysia 2011] Exploit Automation
Skiron - Experiments in CPU Design in D
[Kiwicon 2011] Post Memory Corruption Memory Analysis
Os lab final
Alexander Reelsen - Seccomp for Developers
[CCC-28c3] Post Memory Corruption Memory Analysis
System Calls - Introduction
Program Assignment Process ManagementObjective This program a.docx
process creation OS
OSTEP Chapter2 Introduction
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
04_ForkPipe.pptx
Unix presentation.pdf

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
Understanding_Digital_Forensics_Presentation.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Chapter 3 Spatial Domain Image Processing.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Approach and Philosophy of On baking technology
Understanding_Digital_Forensics_Presentation.pptx

Process management

  • 2. 1.Process Concept: 2.Operations On Process: 3.Inter Process Communication: Contents
  • 3. OS Performs following Actions While creating Process Assign Process Id and Priority Allocating Memory and other resources Set up the Process environment Creating PCB Return Process Id
  • 5. Process ID Priority Process State PSW CPU register Event Information Memory Allocation Resource Held PCB Pointer Process Scheduling Information PSW & CPU reg information Info for which process is waiting Pointer to another PCB Process Control Block(PCB)
  • 7. Parent process create children processes, which, in turn create other processes, forming a tree of processes.  Resource sharing ✦ Parent and children share all resources. ✦ Children share subset of parent’s resources. ✦ Child will not exceed resource limits on that process.  Execution ✦ Parent and children execute concurrently. ✦ Parent waits until children terminate. Process Creation
  • 8. ✦ 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 (Cont.) The system calls used for the creation of the process:
  • 9. 1.A call to fork() will create a completely separate sub- process which will be exactly the same as the parent. 2.The process that initiates the call to fork is called the parent process. 3.The new process created by fork is called the child process. 4.The child gets a copy of the parent's text and memory space. 5.They do not share the same memory . Fork()
  • 10. fork() system call returns an integer to both the parent and child processes: -1 this indicates an error with no child process created. A value of zero indicates that the child process code is being executed. Positive integers represent the child’s process identifier (PID) and the code being executed is in the parent’s process. Fork(contd.)
  • 11. if ( (pid = fork()) == 0) printf(“I am the childn”); else printf(“I am the parentn”); Simple Fork Example
  • 12. Calling one of the exec() family will terminate the currently running program and starts executing a new one which is specified in the parameters of exec in the context of the existing process. The process id is not changed. exec()
  • 13. • int execl( const char *path, const char *arg, ...); • int execlp( const char *file, const char *arg, ...); • int execle( const char *path, const char *arg , ..., char * const envp[]); • int execv( const char *path, char *const argv[]); • int execvp( const char *file, char *const argv[]); Exec family of functions
  • 16. UNIX Pipes • Pipe sets up communication channel between two (related) processes. Two processes connected by a pipe
  • 17. • One process writes to the pipe, the other reads from the pipe. • Looks exactly the same as reading from/to a file. • System call: int fd[2] ; pipe(&fd[0]) ; fd[0] now holds descriptor to read from pipe fd[1] now holds descriptor to write into pipe UNIX Pipes
  • 18. #include <unistd.h> #include <fcntl.h> #include <stdio.h> char *message = "This is a message!!!" ; main() { char buf[1024] ; int fd[2]; pipe(fd); /*create pipe*/ if (fork() != 0) { /* I am the parent */ write(fd[1], message, strlen (message) + 1) ; } else { /*Child code */ read(fd[0], buf, 1024) ; printf("Got this from MaMa!!: %sn", buf) ; } } UNIX Pipes
  • 19. Shared Memory Common chunk of read/write memory among processes Proc. 1 Proc. 2 ptr Attach Proc. 3 Proc. 4 Proc. 5 ptr ptr ptr ptr Attach Create Shared Memory (unique key) 0 MAX
  • 20. Creating Shared Memory int shmget(key_t key, size_t size, int shmflg); Example: key_t key; int shmid; key = ftok(“<somefile>", ‘A'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); Here’s an example: shm_create.c.
  • 21. Attach and Detach Shared Memory void *shmat(int shmid, void *shmaddr, int shmflg); int shmdt(void *shmaddr); Example: key_t key; int shmid; char *data; key = ftok("<somefile>", ‘A'); shmid = shmget(key, 1024, 0644); data = shmat(shmid, (void *)0, 0); shmdt(data); Here’s an shm_attach.c
  • 22. Deleting Shared Memory int shmctl(int shmid, int cmd, struct shmid_ds *buf); shmctl(shmid, IPC_RMID, NULL); Example: Shm_delete.c
  • 23. FIFOs (named pipes) FIFOs are sometimes called named pipes. Pipes can be used only between related processes when a common ancestor has created the pipe. With FIFOs, unrelated processes can exchange data. Creating a FIFO is similar to creating a file. #include <sys/ stat.h> int mkfifo( const char *pathname, mode_t mode); Returns: 0 if OK,-1 on error.
  • 24. Opening a FIFO ● open(pathname, O_RDONLY) ● Open read end ● open(pathname, O_WRONLY) ● Open write end ● open() locks until other end is opened ● Opens are synchronized open(pathname, O_RDONLY | O_NONBLOCK) can be useful
  • 25. Message Queues ● Message-oriented communication ● Receiver reads messages one at a time – No partial or multiple message reads ● Unlike pipes, multiple readers/writers can be useful ● Messages have priorities ● Delivered in priority order ● Message notification feature
  • 26. Message queue operations Int msgget(key_t, int flag) Int msgctl(int msgid, int cmd, struct msgid_ds *buf) Int msgsnd(int msgid, const void *ptr, size nbytes, int flag); Int msgrcv(int msgid, void *ptr, size_t nbytes, long type, int flag);
  • 27. Semaphores • A semaphore is a counter used to provide access to a shared data object for multiple processes. • To obtain a shared resource, a process needs to do the following: 1.Test the semaphore that controls the resource. 1. If the value of the semaphore is positive, the process can use the resource. In this case, the process decrements the semaphore value by 1, indicating that it has used one unit of the resource.
  • 28. 3.If the value of the semaphore is 0, the process goes to sleep until the semaphore value is greater than 0. When the process wakes up, it returns to step 1.
  • 29. • The first function to call is semget to obtain a semaphore ID. #include <sys/ sem.h> int semget(key_t key, int nsems, int flag); Returns: semaphore ID if OK, -1 on error. • The number of semaphores in the set is nsems. If a new set is being created (typically in the server), we must specify nsems. If we are referencing an existing set (a client), we can specify nsems as 0.
  • 30. semget() Gets a semaphore set. The value returned is its id, for use with other calls. int semget(key_t key, int n, int flags); key is the key associated with the semaphore set you want. Don’t think about it—just use IPC PRIVATE. n is the number of semaphores the set should contain. flags specifies how how the set should be allocated. SHM R | SHM W is the best thing to pass.
  • 31. semop() Performs a semaphore operation (i.e. incrementing, decrementing, etc.) on the selected members of a semaphore set. This is one of those ones that should really be a bunch of seperate calls. int semop(int id, struct sembuf* op, unsigned n); id is the semaphore set’s id. op is the operation to perform. n is the number of semaphores to affect. You’ll nearly always be passing in a value of 1 here. struct sembuf’s sem op field is important. It specifies what you want to do to the semaphore, be it incrementing, decrementing, or toasting over an open fire8 . • A non-zero value will be added to the semaphore’s value. Note that this means negative values indicate subtraction. • A value of zero will make the operation block until the semaphore value becomes zero.
  • 32. semctl() A bit like shmctl(), but for semaphores. Again, ridiculously overcomplicated. int semctl(int id, int iSem, int cmd, union semun arg); id is the semaphore set id. iSem is the semaphore you want to twiddle. Only valid with some of the commands. cmd is the command you want to perform. arg is used for fiddling with semaphore values. With everything but cmd set to SETALL, just pass NULL. There are two values for cmd worth looking at: SETALL and IPC RMID. For details on the others, type man semctl.
  • 34. Signals • A signal is a mechanism for notifying a program that some event has occurred. • intuition: signal  “software interrupt” • when a signal is sent to a program, its normal execution is interrupted • depending on (1) the state of the program, and (2) the type of signal, the program may • enter some pre-specified signal handler; or • take some default action.
  • 35. Example Signals (not a complete list) Signal Name Number Description SIGHUP 1 Hangup (POSIX) SIGINT 2 Terminal interrupt (ANSI) SIGQUIT 3 Terminal quit (POSIX) SIGILL 4 Illegal instruction (ANSI) SIGTRAP 5 Trace trap (POSIX) SIGFPE 8 Floating point exception (ANSI) SIGKILL 9 Kill(can't be caught or ignored) (POSIX) SIGSEGV 11 Invalid memory segment access (ANSI) SIGTERM 15 Termination (ANSI) SIGSTKFLT 16 Stack fault SIGSTOP 19 Stop executing(can't be caught or ignored) (POSIX) SIGPROF 27 Profiling alarm clock (4.2 BSD) SIGWINCH 28 Window size change (4.3 BSD, Sun) SIGPWR 30 Power failure restart (System V) … … …
  • 36. Signal Sources a process window manager shell command kernel SIGWINCH SIGKILL SIGINT SIGHUP SIGQUIT SIGALRM SIGPIPE SIGUSR1
  • 37. Synchronous vs. Asynchronous Signals • Synchronous signals: • arise from executing an instruction in the process’s instruction stream • e.g.: illegal instruction (SIGILL); illegal address (SIGSEGV) • causes a trap into the OS kernel trap handler • sometimes referred to as “traps” • directed to the process/thread that executed the instruction • Asynchronous signals: • source is external to the current execution • e.g.: profiling clock (SIGPROF); terminal interrupt, ^C (SIGINT)
  • 38. Signal Handling • Use the signal handling library: signal.h • Then can use the signal call: #include <signal.h> void (*signal( int sig, void (*handler)(int))) (int) ; • signal returns a pointer to the PREVIOUS signal handler • #include <signal.h> typedef void Sigfunc(int); /* my defn */ Sigfunc *signal( int signo, Sigfunc *handler ); Signal is a function that takes two arguments: sig and handler The signal to be caught or ignored is given as argument sig The function to be called when the specified signal is received is given as a pointer to the function handler The handler function Receives a single integer Argument and returns void The signal function itself returns a pointer to a function. The return type is the same as the function that is passed in, i.e., a function that takes an int and returns a void The returned function takes a integer parameter.
  • 41. Behind the scenes of a SIGSEGV • When a program tries to access a bad address: • execution traps into the OS kernel • if no handler is specified: • kernel invokes the default handler • default handler prints out “Segmentation fault” and kills the process • if a handler is specified: • kernel executes the handler • the expectation is that the handler fixes the problem • restarts the offending operation • this allows programmer-controlled recovery from errors
  • 42. Sending signals • A program can send a signal to another program using the kill() system call: int kill(pid_t pid, int sig) sends the signal number sig to process pid (see /usr/include/asm-generic/signal.h) • A user can send a signal from the command line using the kill command: kill –N pid E.g., “kill -9 pid” (9 = SIGKILL)
  • 43. Sending Signals via Function Call raise() int raise(int iSig); • Commands OS to send a signal of type iSig to current process • Returns 0 to indicate success, non-0 to indicate failure Example int ret = raise(SIGINT); /* Process commits suicide. */ assert(ret != 0); /* Shouldn't get here. */
  • 44. Sending Signals via Function Call kill() int kill(pid_t iPid, int iSig); • Sends a iSig signal to the process whose id is iPid • Equivalent to raise(iSig) when iPid is the id of current process • Editorial comment: Better function name would be sendsig() Example pid_t iPid = getpid(); /* Process gets its id.*/ kill(iPid, SIGINT); /* Process sends itself a SIGINT signal (commits suicide?) */
  • 45. • Suspend the calling process until a signal is caught. • #include <unistd.h> int pause(void); • Returns -1 with errno assigned EINTR. (Linux assigns it ERESTARTNOHAND). • pause() only returns after a signal handler has returned. pause()
  • 46. alarm() • Set an alarm timer that will ‘ring’ after a specified number of seconds • a SIGALRM signal is generated • #include <unistd.h> long alarm(long secs); • Returns 0 or number of seconds until previously set alarm would have ‘rung’.
  • 47. Installing a Signal Handler signal() sighandler_t signal(int iSig, sighandler_t pfHandler); • Installs function pfHandler as the handler for signals of type iSig • pfHandler is a function pointer: typedef void (*sighandler_t)(int); • Returns the old handler on success, SIG_ERR on error • After call, pfHandler is invoked whenever process receives a signal of type iSig
  • 48. Installing a Handler Example 2 Program testsignalall.c: #define _GNU_SOURCE #include <stdio.h> #include <assert.h> #include <signal.h> static void myHandler(int iSig) { printf("In myHandler with argument %dn", iSig); } …
  • 49. Installing a Handler Example 2 (cont.) Program testsignalall.c (cont.): … int main(void) { void (*pfRet)(int); pfRet = signal(SIGHUP, myHandler); /* 1 */ pfRet = signal(SIGINT, myHandler); /* 2 */ pfRet = signal(SIGQUIT, myHandler); /* 3 */ pfRet = signal(SIGILL, myHandler); /* 4 */ pfRet = signal(SIGTRAP, myHandler); /* 5 */ pfRet = signal(SIGABRT, myHandler); /* 6 */ pfRet = signal(SIGBUS, myHandler); /* 7 */ pfRet = signal(SIGFPE, myHandler); /* 8 */ pfRet = signal(SIGKILL, myHandler); /* 9 */ …
  • 50. Installing a Handler Example 2 (cont.) Program testsignalall.c (cont.): … /* Etc., for every signal. */ printf("Entering an infinite loopn"); for (;;) ; return 0; }