System Calls & Signals. setsockopt n Used to set socket options n SO_LINGER - Sets or gets the SO_LINGER option. The argument is a linger structure. n.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Stat, mmap, process syncing and file system interface Nezer J. Zaidenberg.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 17 Outline Named pipes Signals Lecture 17
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
CSSE Operating Systems
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Files and Directories File types stat functions for file information
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Operating Systems Recitation 4, April th, 2002 Signals.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
Stat mmap and file system interface Nezer J. Zaidenberg.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
System calls for Process management Process creation, termination, waiting.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Operating Systems Inter-Process Communication Signals Moti Geva
Week 12 - Thursday CS222.
Unix Process Management
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
System Calls & Signals.
Lecture 5: Process Creation
Fork and Exec Unix Model
Operating System Hebrew University Spring 2004
CSC Advanced Unix Programming, Fall 2015
Measuring Program Performance Matrix Multiply
File Structure Related system calls
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Chien-Chung Shen CIS, UD
Process Description and Control in Unix
CSCE Systems Programming
Process Description and Control in Unix
Measuring Program Performance Matrix Multiply
Presentation transcript:

System Calls & Signals

setsockopt n Used to set socket options n SO_LINGER - Sets or gets the SO_LINGER option. The argument is a linger structure. n struct linger { n int l_onoff; /* linger active */ n int l_linger; /* how many seconds to linger for */ n }; When enabled, a close(2) or shutdown(2) will not return until all queued messages for the socket have been successfully sent or the linger timeout has been reached. int result; struct linger linger; linger.l_onoff = 1; /*0 = off (l_linger ignored), nonzero = on */ linger.l_linger =1; /* how many seconds to linger for */ result = setsockopt(s, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));

Reuseaddr n Normally you have to wait for about 2 minutes to reuse the same port on a machine n This wait is intended to protect the TCP connection protocol from errors n You can override it with reuseaddr int optval = 1; setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); bind (s, &sin, sizeof(sin));

stat, fstat, lstat #include int stat(const char *pathname, struct stat *buf) int fstat(int fd, struct stat *buf) int lstat(const char *pathname, struct stat *buf)

stat int stat(const char *pathname, struct stat *buf) n Obtains information about the file pointed to by pathname. n Read, write or execute permission of the named file is not required, but all directories listed in the path name leading to the file must be searchable.

fstat int fstat(int fd, struct stat *buf) n Obtains the same information about an open file known by the file descriptor fd. int lstat(const char *pathname, struct stat *buf) n like stat() except in the case where the named file is a symbolic link, in which case lstat() returns information about the link, while stat() returns information about the file the link references.

struct stat Struct stat { mode_t st_mode;/* file type and mode (type & permissions) */ ino_t st_ino; /* inode’s number */ dev_t st_dev; /* device number (file system) */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes */ time_t st_atime; /* last access */ time_t st_mtime; /* last modified */ time_t st_ctime; /* last file status change */ long st_blksize; /* I/O block size */ long st_blocks; /* number of blocks allocated */ }

HTTPD and Stat n You need to use stat in order to determine the type of a file, so you know whether to print out the directory if it doesn’t have a index.html file n You need to use stat to determine the content-length to send back on static files. int err = stat(fullpath, &fstats); if (err != 0) { logmessage = (char *)malloc(MAX_MSG_SZ); sprintf(logmessage,"Thread %d: [%s not found - replaced with %s]", threadnum, fullpath, gDocumentNotFoundPage); logQ->Enqueue(logmessage); // If the path is to a non-existent file, replace it with gDocumentNotFoundPage strcpy(fullpath, gDocumentNotFoundPage); sprintf(responseCode, "404 Document Not Found"); } if (fstats.st_mode & S_IFDIR) { AppendDefaultPage(fullpath, url); }

Signals n Introduced in UNIX systems to simplify IPC. n Used by the kernel to notify processes of system events. n A signal is a short message sent to a process, or group of processes, containing the number identifying the signal.

Example Signals n Linux supports 31 non-real-time signals. n POSIX standard defines a range of values for RT signals: SIGRTMIN 32 … SIGRTMAX (_NSIG-1) in

Signal Transmission n Signal sending: n Kernel updates descriptor of destination process. n Signal receiving: n Kernel forces target process to “handle” signal. n Pending signals are sent but not yet received. n Up to one pending signal per type for each process, except for POSIX.4 signals. n Subsequent signals are discarded. n Signals can be blocked, i.e., prevented from being received.

Signal-Related Data Structures n sa_handler specifies the action to be associated with signum. This function receives the signal number as its only argument. n sa_mask gives a mask of signals which should be blocked during execution of the signal handler n sa_flags specifies a set of flags which modify the behavior of the signal handling process (SA_RESTART) struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); }

If you want more information siginfo_t { int si_signo; /* Signal number */ int si_errno; /* An errno value */ int si_code; /* Signal code */ pid_t si_pid; /* Sending process ID */ uid_t si_uid; /* Real user ID of sending process */ int si_status; /* Exit value or signal */ clock_t si_utime; /* User time consumed */ clock_t si_stime; /* System time consumed */ sigval_t si_value; /* Signal value */ int si_int; /* POSIX.1b signal */ void * si_ptr; /* POSIX.1b signal */ void * si_addr; /* Memory location which caused fault */ int si_band; /* Band event */ int si_fd; /* File descriptor */ }

Signal Handling System Calls n int sigaction(int sig, const struct sigaction *act, struct sigaction *oact); Replaces the old signal() function. n Used to bind a handler to a signal. n Handler should match either n void handler(int); n void handler(int, siginfo_t *info, ucontext_t *uap); n The handler function should match this prototype if the SA_SIGINFO bit is set in flags. It then should be pointed to by the sa_sigaction member of struct sigaction.

#include void *functionC(void *ptr); void handler (int status); /* definition of signal handler */ int counter = 0; main() { int rc1, rc2; pthread_t thread1, thread2; // First set up the signal handler struct sigaction sigold, signew; signew.sa_handler=handler; sigemptyset(&signew.sa_mask); sigaddset(&signew.sa_mask,SIGINT); signew.sa_flags = SA_RESTART; sigaction(SIGINT,&signew,&sigold); /* Create independent threads each of which will execute functionC */ if( (rc1=pthread_create( &thread1, NULL, &functionC, (void *)1)) ) { printf("Thread creation failed: %d\n", rc1); } if( (rc2=pthread_create( &thread2, NULL, &functionC, (void *)2)) ) { printf("Thread creation failed: %d\n", rc2); } printf("I am parent thread %d[%u]\n", 0,(unsigned int)pthread_self()); pthread_join( thread1, NULL); pthread_join( thread2, NULL); } void handler (int status) { printf("%u received signal %d\n", (unsigned int)pthread_self(), status); } void *functionC(void *ptr) { int thnum = (int)ptr; for(;;) { sleep(1); counter++;// Note: This should be protected by semaphores printf("I am thread %d[%u] counter %d\n",thnum,(unsigned int)pthread_self(), counter); }

Signal Handlers n To get more information //void handler (int status, siginfo_t *info, ucontext_t *uap); void handler (int status, struct __siginfo *info, void *uap); ……. // First set up the signal handler struct sigaction sigold, signew; signew.sa_sigaction=handler; sigemptyset(&signew.sa_mask); sigaddset(&signew.sa_mask,SIGINT); signew.sa_flags = SA_RESTART | SA_SIGINFO; sigaction(SIGINT,&signew,&sigold); ……. //void handler (int status, siginfo_t *info, ucontext_t *uap) void handler (int status, struct __siginfo *info, void *uap) { printf("[%d]%u received signal %d from %d\n", getpid(), (unsigned int)pthread_self(), status, info->si_pid); }

Sending a Signal n int kill(pid_t pid, int sig); n Causes the signal to be delivered to another process n Example: n kill(pID,SIGKILL); n Signals are defined in /usr/include/sys/signal.h n Example usage: n kill the child you execed if it runs too long n How do we tell if a child runs too long?

Timers n int getitimer(int which, struct itimerval *value); n int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue); n Provide microsecond granularity n Will send SIGALRM every interval struct itimerval { struct timeval it_interval; /* next value */ struct timeval it_value; /* current value */ }; struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };

What have your kids been up to? n int getrusage(int who, struct rusage *usage); n Reports on resource usage by yourself and your children n Also set in n pid_t wait4(pid_t pid, int *stat_loc, int options, struct rusage *rusage); struct rusage { struct timeval ru_utime; /* user time used */ struct timeval ru_stime; /* system time used */ long ru_maxrss; /* maximum resident set size */ long ru_ixrss; /* integral shared memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims */ long ru_majflt; /* page faults */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* messages sent */ long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ };

#include using namespace std; void handler (int status); /* definition of signal handler */ // Global variable so signal handler can see it pid_t pID; // This example shows how to use execve and signals main() { struct sigaction sigold, signew; struct itimerval rttimer; struct itimerval old_rttimer; signew.sa_handler=handler; sigemptyset(&signew.sa_mask); sigaddset(&signew.sa_mask,SIGCHLD); sigaddset(&signew.sa_mask,SIGALRM); signew.sa_flags = SA_RESTART; sigaction(SIGCHLD,&signew,&sigold); sigaction(SIGALRM,&signew,&sigold); // Now set up the interval timer rttimer.it_value.tv_sec = 10; rttimer.it_value.tv_usec = 0; rttimer.it_interval.tv_sec = 10; rttimer.it_interval.tv_usec = 0; setitimer (ITIMER_REAL, &rttimer, &old_rttimer); pID = fork(); if (pID == 0) // child { // Code only executed by child process // exec the program char *argvToChild[5]; char *envToChild[5]; argvToChild[0] = "foo"; argvToChild[1] = "bar"; argvToChild[2] = NULL; envToChild[0] = "SERVER_NAME=gunga"; envToChild[1] = "GUMBO=GIMBO"; envToChild[2] = NULL; printf("Child about to exec\n"); execve("loop.pl",argvToChild,envToChild); perror("Child should not return"); } else // parent { // Code only executed by parent process for(;;); } void handler (int status) { int stat; int err; struct rusage child_ru; printf("received signal %d\n",status); if(status == SIGCHLD) { err = wait4(pID,&stat,0,&child_ru); printf("wait returned %d status %X exit status %d\n",err,stat, WEXITSTATUS(stat)); printf("user time %d, system time %d\n",child_ru.ru_utime.tv_sec,child_ru.ru_stime.tv_sec); } if(status == SIGALRM) { printf("This guy %d is toast\n",pID); kill(pID,SIGKILL); }