1 / 22

COP4600- Discussion Session 3- System Calls

COP4600- Discussion Session 3- System Calls. Mahendra Kumar makumar@cise.ufl.edu. Outline. What is a system call? Pipe Overview of Process related system calls. Signals Files File related system calls. What is a system call?. Interface between the OS and the application program.

siusan
Download Presentation

COP4600- Discussion Session 3- System Calls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP4600- Discussion Session 3- System Calls Mahendra Kumar makumar@cise.ufl.edu

  2. Outline • What is a system call? • Pipe • Overview of Process related system calls. • Signals • Files • File related system calls

  3. 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.

  4. 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.

  5. 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?

  6. 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.

  7. 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"); }

  8. Some common Process system calls • Fork() • Waitpid() • Brk() • Getpid() • Execve()

  9. 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.

  10. 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

  11. 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

  12. 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.

  13. 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

  14. 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?

  15. 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)

  16. Dup() diagram

  17. main() { 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); } Dup() example

  18. 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 */ }

  19. 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

  20. 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.

  21. I-node diagram

  22. Questions?

More Related