Title: Solaris System Calls
1Solaris System Calls
- Solaris has about 200 system calls.
- Files and I/O
- Process management
- Interprocess communication
- Others
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
2System Calls Files I/O
- open, close open and close a file
- create, unlink create and remove a file
- read, write read and write a file
- lseek move to a specified byte in a file
- chmod change access permission mode
- mkdir, rmdir make and remove a directory
- stat get file status
- ioctl control device
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
3System Calls Process Management
- fork create a new process
- exec execute a file
- exit terminate proces
- wait wait for a child process to terminate
- sbrk change the size of the space allocated in
the heap data segment of the process (used by
memory allocation functions, e.g. malloc)
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
4System Calls Interprocess Communication
- kill, sigsend send a signal to a process
- pause suspend process until signal
- sigaction set signal handler
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
5- Process Creation and the UNIX fork
- include ltsys/types.hgt include ltunistd.hgt
- pid_t fork(void)
- fork is a system call to create a new process
- fork copies the parent's memory image to the
child process - parent and child processes continue execution at
the instruction after fork() - the child inherits some of the parent's
resources such as opened files and devices
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
6- fork () return value
-
- After fork(), parent and child processes are
completely identical except the return values of
fork - fork() returns the child's process ID to the
parent - fork() returns 0 to the child process
- by checking the return value, both processes
know who they are and may execute different code
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
7Get process id getpid() and getppid()
- getpid() returns the process id of the calling
process - getppid() returns the parent process id of the
calling process
8- include ltsys/types.hgt include ltunistd.hgt
- x 0 fork() x 1
- After fork, parent and child processes continue
executing the same code, i.e. x 1 - Each process has its own copy of the x variable.
- Both processes are not distinguishable because
forks return value is not tested.
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
9include ltstdio.hgt include ltsys/types.hgt
include ltunistd.hgt if ((childpid fork())
0) fprintf(stderr, "I am the child, ID
ld\n", (long)getpid()) /child
code goes here/ x 1 else if (childpid
gt 0) fprintf(stderr, "I am the parent, ID
ld\n", (long)getpid())
/parent code goes here/ x 2
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
10- The original process becomes the parent process
and the created process becomes the child process
- In the parent process, fork returns the childs
process ID. - In the child process, fork returns zero.
- Both processes execute different code after
fork. Child process does x1, parent does x2.
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
11Int i int n pid_t childpid for(i1 iltn i)
if (childpid fork()) break fprintf(stderr,
process ld with parent ld\n,
(long)getpid(), (long)getppid()) This program
creates a chain of processes.
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
12 wait System Call include ltsys/types.hgt
include ltsys/wait.hgt pid_t wait(int
stat_loc) pid_t waitpid(pid_t pid, int
stat_loc, int options)
- wait causes the caller process to pause until a
child terminates. - waitpid causes the caller process to pause until
a particular child terminates.
13 include ltsys/types.hgt include ltsys/wait.hgt
include lterrno.hgt int status pid_t childpid
pid_t waitreturn while(childpid ! (waitreturn
wait(status))) if ((waitreturn -1)
(errno ! EINTR)) break
This code segment loops until the child with
process ID childpid complete or until there is an
error.
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
14wait return value
- wait returns the process ID of the child that
terminates. - wait returns -1 and set the errno variable if the
process has no children left or the process
receives a signal. - stat_loc points to return status of the child.
The child returns its status by calling exit or
return.
15/ forkdemo1.c shows how fork creates two
processes, distinguishable by the different
return values from fork() / include ltstdio.hgt
main() int ret_from_fork, mypid mypid
getpid() / who am i? / printf("Before
my pid is d\n", mypid) / tell the
world / ret_from_fork fork() sleep(1) pr
intf("After my pid is d, fork() said
d\n", getpid(), ret_from_fork)
16/ forkdemo2.c - shows how child processes pick
up at the return from fork() and
can execute any code they like,
even fork(). Predict number of lines of output.
/ main() printf("my pid is d\n", getpid()
) fork() fork() fork() printf("my pid is
d\n", getpid() )
17/ forkdemo3.c - shows how the return value from
fork() allows a process to
determine whether it is a child
or process / include ltstdio.hgt main() int
fork_rv printf("Before my pid is d\n",
getpid()) fork_rv fork() / create new
process / if ( fork_rv -1 ) / check for
error / perror("fork") else if ( fork_rv
0 ) printf("I am the child. my pidd\n",
getpid()) else printf("I am the parent. my
child is d\n", fork_rv)
18/ waitdemo1.c - shows how parent pauses until
child finishes / include ltstdio.hgt define DE
LAY 2 main() int newpid void child_code(),
parent_code() printf("before mypid is d\n",
getpid()) if ( (newpid fork()) -1
) perror("fork") else if ( newpid 0
) child_code(DELAY) else parent_code(newpid)
19 / new process takes a nap and then exits
/ void child_code(int delay) printf("child d
here. will sleep for d seconds\n", getpid(),
delay) sleep(delay) printf("child done. about
to exit\n") exit(17) / parent waits for
child then prints a message / void
parent_code(int childpid) int wait_rv /
return value from wait() / wait_rv
wait(NULL) printf("done waiting for d. Wait
returned d\n", childpid, wait_rv)