cop4600 discussion session 3 system calls
Download
Skip this Video
Download Presentation
COP4600- Discussion Session 3- System Calls

Loading in 2 Seconds...

play fullscreen
1 / 22

COP4600- Discussion Session 3- System Calls - PowerPoint PPT Presentation


  • 108 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' COP4600- Discussion Session 3- System Calls' - siusan


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
outline
Outline
  • What is a system call?
  • Pipe
  • Overview of Process related system calls.
  • Signals
  • Files
  • File related system calls
what is a system call
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.
slide4
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
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
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
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
Some common Process system calls
  • Fork()
  • Waitpid()
  • Brk()
  • Getpid()
  • Execve()
signals
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
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
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
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
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
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
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 example
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
dup contd
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
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
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.
ad