COP4600- Discussion Session 3- System Calls
description
Transcript of COP4600- Discussion Session 3- System Calls
![Page 2: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/2.jpg)
Outline What is a system call? Pipe Overview of Process related
system calls. Signals Files File related system calls
![Page 3: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/3.jpg)
What is a system call? Interface between
the OS and the application program.
Mechanism used by an application program to request service from the OS.
![Page 4: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/4.jpg)
Pipe Shared in-memory file between two processes.
This common file is used by the two processes to communicated with each other, i.e. to have interprocess communication.
First process is always supposed to write to the pipe and the second process is supposed to read from the pipe.
![Page 5: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/5.jpg)
Pipe (Contd.) Command to create a pipe
who | sort Another example:
who | sort | lpr How many pipes in the above example? Which processes read and write to which pipes?
![Page 6: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/6.jpg)
Overview of Process related system calls Fork()- creates a new process.
Creates an exact duplicate of the original process.
The new process is called the child process.
Return value of fork() 0 in the child process. PID(non zero) of child in the parent
process.
![Page 7: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/7.jpg)
Fork()- Contd.#include <stdio.h>void ChildProcess(void); /* child process prototype */ void ParentProcess(void); /* parent process prototype */ void main(void) { int pid; int status; pid = fork(); if (pid == 0)
ChildProcess();waitpid(-1,&status,0);
else ParentProcess();
} void ChildProcess(void) { int i; for (i = 1; i <= 2; i++)
printf("child”); }void ParentProcess(void) { int i; for (i = 1; i <= 2; i++)
printf("parent");}
![Page 8: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/8.jpg)
Some common Process system calls Fork() Waitpid() Brk() Getpid() Execve()
![Page 9: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/9.jpg)
Signals Act as a Software interrupt to a process. notifications sent to a process in order
to notify it of various "important" events.
If a signal arrives at a process and the process has no action for that signal, the process is killed.
To avoid a process being killed unexpectedly, signal must be handled.
![Page 10: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/10.jpg)
Signals-Contd. Some system calls for signals:
Sigaction(type, &action, &oldact); Sigaction(SIGINT,SIG_IGN,NULL); SIGKILL cannot be caught or ignored.
Sigreturn(&context); Sigprocmask(how,&set,&old); Sigpending(set); Sigsuspend(sigmask); kill
![Page 11: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/11.jpg)
Files Every file can be accessed with the
path from the root directory. What is the difference between the
two commands? Cd /cise/homes/james Cd cise/homes/james
![Page 12: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/12.jpg)
Files-Contd. There are two types of files in minix
General files Special files
Block special files Character special files
Special files are kept in /dev directory.
Example: printer, hard disk etc are all represented as files in minix/unix.
![Page 13: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/13.jpg)
File related system calls Used for creating, reading and writing files. Creat() : creates a file.
Fd=creat(“abc.txt”,0754); Creat() is obsolete now. Open() - used to create and open the file at the
same time. Fd=open(“abc.txt”,O_RDONLY);
Close() - to close the file Read() – to read from a file Write() – to write to a file Lseek() - used to change the value of position
pointer
![Page 14: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/14.jpg)
Lseek() long lseek(int fd,off_t offset,int whence)
Fd: file descriptor Offset: the added offset which is defined by whence. Whence:
SEEK_SET: set pointer to value of offset. SEEK_CUR: set the pointer to its current value plus
the value of offset. SEEK_END: set the pointer to the size of the file plus
the value of offset .
Lseek() returns the new value of the position pointer.
How to get the current position of the file?
![Page 15: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/15.jpg)
Dup() system call Duplicates a file descriptor fd. Points to the same file table entry as
the first file descriptor, fd. Properties:
Same open file (or pipe) Same file pointer (that is, both file
descriptors share one file pointer) Same access mode (read, write, or
read/write)
![Page 16: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/16.jpg)
Dup() diagram
![Page 17: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/17.jpg)
Dup() examplemain() { int fd1, fd2; fd1 = open("file2", O_WRONLY);
fd2 = open("file2", O_WRONLY); write(fd1, "Jim\n", strlen("Jim\n")); write(fd2, "Plank\n", strlen("Plank\n"));
/*code to print out the contents if file2 */
close(fd1);close(fd2);
}
main() {
int fd1, fd2; fd1 = open("file2", O_WRONLY);
fd2 = dup(fd1); write(fd1, "Jim\n", strlen("Jim\n")); write(fd2, "Plank\n", strlen("Plank\n"));
/*code to print out the contents if file2 */
close(fd1);close(fd2);
}
![Page 18: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/18.jpg)
Dup() contd. Helpful in redirection of standard output
to a file. Example:
int main() { int fd; fd = open("foo.bar",O_WRONLY | O_CREAT, S_IREAD | S_IWRITE );close(1); dup(fd); /* fd will be duplicated into standard out's slot */ close(fd); /* close the extra slot */ printf("Hello, world!\n"); /* should go to file foo.bar */ exit (0); /* exit() will close the files */
}
![Page 19: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/19.jpg)
Ioctl() system call Used by block device drivers to
control CD-ROM devices. Mainly used with special character
file like terminals. Three modes:
Raw mode Cooked mode Cbreak mode
![Page 20: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/20.jpg)
File directory management Mkdir() – creates a directory Rmdir() – removes a directory Link() – used to create a symbolic link in
another directory. I-node of the file remains the same with link
command. The name of the file is only changed. Every file is identified by a unique I-node
number.
![Page 21: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/21.jpg)
I-node diagram
![Page 22: COP4600- Discussion Session 3- System Calls](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813ab0550346895da2b072/html5/thumbnails/22.jpg)
Questions?