Session 2: System Calls - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Session 2: System Calls

Description:

Basically, the shell displays a prompt character ($, %, etc) ... before returning; it returns the first hunk of data that appears, no matter how short it is. ... – PowerPoint PPT presentation

Number of Views:112
Avg rating:3.0/5.0
Slides: 40
Provided by: cise8
Category:
Tags: calls | hunk | session | system

less

Transcript and Presenter's Notes

Title: Session 2: System Calls


1
Session 2 System Calls
  • COP 4600 Operating System
  • Instructor Tuba Yavuz
  • TA Xiao Li
  • xl1_at_cise.ufl.edu

2
Session Outline
  • Introduction to shells.
  • Introduction to system calls
  • System call fork()
  • System call pipe()
  • System call execvp()
  • System call dup() dup2()

3
Session Outline
  • Introduction to shells.
  • Introduction to system calls
  • System call fork()
  • System call pipe()
  • System call execvp()
  • System call dup() dup2()

4
Shells
  • In this assignment you are going to modify a C
    program that simulates a very simple shell.
  • Q What is an OS shell ?
  • A A user interface of OS.
  • A graphical user interface (GUI),
  • e.g., Windows Explorer
  • A command line interface (CLI),
  • e.g., Unix Shells

5
Shells
  • Basically, the shell displays a prompt character
    (, , etc) and waits for a command from the
    user. Once the user inputs the command and hits
    the enter key, the shell creates some child
    processes each of which executes a command
    entered on the command line.

fork
execvp
6
Shells
  • Question
  • Unix shells history?
  • What UNIX shells are available?
  • What are the differences between them?
  • How do you change your interactive shell?
  • Reference
  • http//www.faqs.org/faqs/unix-faq/shell/shell-diff
    erences/

7
Shells
  • This very simple shell that you are going to
    modify creates just a single child process to
    execute the command and once the child terminates
  • it displays the prompt character again and waits
    for

wait
do while
another command and so on. Since this shell is
very simple it does not support redirection or
pipes.
8
Shells - pipes
  • In this assignment your task is to add some code
    to this simple shell to provide the pipe feature
    that a standard shell has.

9
Session Outline
  • Introduction to shells.
  • Introduction to system calls
  • System call fork()
  • System call pipe()
  • System call execvp()
  • System call dup() dup2()

10
System Call the concept
  • Question What are system calls?
  • Answer
  • The services provided by the kernel of OS.
  • The only gate to the hardware.
  • Examples allocating memory space, reading from
    or writing to a file, printing text on screen

11
System Call the concept
  • Question Why are system calls needed?
  • Answer
  • Share The access of hardware must be
    synchronized.
  • Stability Security Some activities may fail
    the system or even destroy the hardware.

12
System Call process management
  • Four groups
  • Process management
  • File management
  • Directory and file system management
  • Miscellaneous

13
System Call process management
  • Question Why OS introduces the concept
    process?
  • Process a program in execution.
  • Single-tasking OS running only one application
    at a time. A new one starts only, after the last
    one has finished. (MS-DOS)
  • Answer For implementing the multitasking
    capability, different processes are switched
    quickly by OS. (Windows, Unix)
  • Process Management!!!

14
System Call process status

new The process is being created. running
Instructions are being executed. waiting The
process is waiting for some event to occur.
ready The process is waiting to be assigned to
a processor. terminated The process has
finished execution.
15
Session Outline
  • Introduction to shells.
  • Introduction to system calls
  • System call fork()
  • System call pipe()
  • System call execvp()
  • System call dup() dup2()

16
Fork() system call
  • Task 3) To create a number of child processes
    using fork()
  • Solution How to create child processes?
    (function fork() )
  • for(i from 0 to numOfProcesses)
  • processID fork() // Create a child process
  • if (processID 0) // Child
  • break
  • if (processID 0) // Child
  • do_something_in_Child
  • else // Parent
  • do_something_in_Parent
  • Listing. Creating child processes

17
Fork() system call
  • processID fork() // Create a child process
  • if (processID 0) // Child
  • else if (processID -1) // Error
  • else // Parent
  • The new process (the child process) is an exact
    copy of all the segments of the calling process
    (the parent process).
  • Both the parent and child processes may execute
    independently of each other.
  • In the child process, the return value of fork()
    is 0, whereas the return value in the parent
    process is the PID of the newly-created child
    process.

fork()
Parent
Child
18
Session Outline
  • Introduction to shells.
  • Introduction to system calls
  • System call fork()
  • System call pipe()
  • System call execvp()
  • System call dup() dup2()

19
Pipes
  • Solution How to exchange the data between the
    parent process and the child process? (
    function pipe() )
  • Fff

Can we use the shared variables?
20
Pipes
  • A pipe is a method of making the communication
    between two processes looks like file read and
    write.
  • One process writes to the pipe, the other reads
    from the pipe.
  • Looks exactly the same as reading from/to a file.

21
Pipes
  • Int fds2
  • pipe (fds)
  • int processID fork()
  • Note The pipe() call must be made BEFORE a call
    to fork(), or the descriptors will not be
    inherited by the child!

22
Pipes
  • FIFO Any data written to fds1 will show up in
    fds0 in the same order it was written.

23
Pipes
  • Example The parent wants to write down a
    pipeline to a child.
  • The parent closes its read end, and writes into
    the other end.
  • The child closes its write end and reads from the
    other end.

pfd0 which file descriptor to use content
where to store the read values SIZE how many
characters to read
Note from the Application Programmer's
viewpoint.
pfd1 which file descriptor to use content what
to write SIZE how many characters to write
24
Pipes
  • Question Why should parent close the read end
    and child close the write end?
  • Answer
  • Before fork,
  • After fork,

Reference http//www.utdallas.edu/kcooper/teachi
ng/3375/Tutorial6a/tutorial6.htm
25
Pipes
  • If (the writer close the end pfd1)
  • the reader will get 0 bit from the pipe.
  • Else
  • If the pipe is empty, the reader will have to
    wait until some other process puts data in the
    pipe.
  • Note the read(pfd0, content, SIZE) call
    doesn't wait for (SIZE) bytes to show up in the
    pipe before returning it returns the first hunk
    of data that appears, no matter how short it is.

26
Pipes
  • A pipe is usually a one-way (half-duplex)
    communication only.
  • For two-way (full-duplex) communication between
    processes, two pipes can be set up, one for each
    direction.

27
  • Parent sends directory name to child. Child
    opens and reads file, then returns results to
    parent.

1) parent creates pipe 1, pipe 2 2) fork 3)
parent closes read end of pipe1 4) parent closes
write end of pipe2 5) child closes write end of
pipe1 6) child closes read end of pipe2
parent process
child process
fork
readfd
readfd
writefd
writefd
pipe1
flow of data
pipe2
flow of data
kernel
28
Session Outline
  • Introduction to shells.
  • Introduction to system calls
  • System call fork()
  • System call pipe()
  • System call execvp()
  • System call dup() dup2()

29
Shells
  • This very simple shell that you are going to
    modify creates just a single child process to
    execute the command and once the child terminates
  • it displays the prompt character again and waits
    for

wait
do while
another command and so on. Since this shell is
very simple it does not support redirection or
pipes.
30
execvp() system calls
  • int execvp(const char file, char const
    argv)
  • file an executable file path.
  • the search path specified in the environment by
    the PATH variable
    (its default value is
    bin/usr/bin).
  • e.g.,

31
Session Outline
  • Introduction to shells.
  • Introduction to system calls
  • System call fork()
  • System call pipe()
  • System call execvp()
  • System call dup() dup2()

32
File descriptors
33
dup() dup2() system calls
  • Create a copy of the file descriptor oldfd
  • Points to the same file table entry as the first
    file descriptor, oldfd.

34
dup() system call
  • 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)

35
dup() system call
  • Helpful in redirection of standard output to a
    file.
  • Example

Both descriptors can be used interchangeably.
Typically, we close one of the standard streams
first.
36
dup() system call
1
1
1
37
dup2() system call
  • Example

38
dup2() system call
temp
temp
temp
temp
39
Any Question?
Write a Comment
User Comments (0)
About PowerShow.com