cop4600- discussion session 3- system calls
DESCRIPTION
COP4600- Discussion Session 3- System Calls. Mahendra Kumar [email protected]. 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. - PowerPoint PPT PresentationTRANSCRIPT
COP4600- Discussion Session 3- System Calls
Mahendra [email protected]
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.
Mechanism used by an application program to request service from the OS.
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.
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?
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.
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");}
Some common Process system calls Fork() Waitpid() Brk() Getpid() Execve()
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.
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
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
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.
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
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?
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)
Dup() diagram
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);
}
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 */
}
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
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.
I-node diagram
Questions?