Lecture 8 Exec Family - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Lecture 8 Exec Family

Description:

per process open file table. signal table. 6. CSCE 510 ... open files, IDs, Uids not pids. cwd, root directory, umask, signal mask and table, environment, ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 27
Provided by: mantonm5
Category:
Tags: exec | family | lecture | open | table

less

Transcript and Presenter's Notes

Title: Lecture 8 Exec Family


1
Lecture 8Exec Family
CSCE 510 Systems Programming
  • Topics
  • Tsearch revisited
  • Gcc
  • Ar implementation
  • make
  • Time functions

September 13, 2005
2
  • Last Time
  • Process IDs getpid, getppid, setuid
  • Creating Processes
  • fork, parent, child, init, orphans, zombies
  • Group IDs
  • Memory layout of Unix Processes
  • Printargs.c, printenv.c, getnev, putenv
  • Standard fork Skeleton
  • Today
  • Processes
  • Gnu Debugger

3
A common C Mistake
  • include ltstat.hgt
  • main()
  • struct stat statbuf
  • if (stat(error.c, statbuf) lt 0)
  • fprintf(stderr, Error\n)
  • exit(1)
  • printf(mode o\n, statbuf-gt st_mode)

4
Standard fork Skeleton
  • Creating new processes fork
  • Standard fork Skeleton
  • if((p fork()) lt 0)
  • / error /
  • else if (p 0) / child process code /
  • else / parent process code /

5
What's copied when a process forks?
  • all variables, data space, heap stack etc.
  • per process open file table
  • signal table
  • ...

6
Properties Inherited across forks
  • Properties Inherited across fork?
  • open files,
  • IDs, Uids not pids
  • cwd,
  • root directory,
  • umask,
  • signal mask and table,
  • environment,
  • shared memory segments

7
Properties Inherited across forks
  • Which properties are different in the child?
  • return value of fork,
  • PID, PPID,
  • times,
  • file locks,
  • pending signals

8
  • Fork example parentChildren.c
  • include
  • main()
  • int pid, pid2, status
  • if ((pid fork()) lt 0) fatal("Cannot fork")
  • else if (pid 0) / Child code /
  • printf("Child code\n")
  • if ((pid fork()) lt 0)fatal("Cannot fork")
  • else if (pid 0) / Child of Child code /
  • printf("Child of Child code\n") sleep(1)
    exit(30)
  • else / Parent of Child code /
  • printf("Parent of Child code\n") sleep(5)
    exit(31)
  • else / Parent Code /
  • while( pid2 wait(status) gt 0)
  • printf("\n pidd \n return value d \n status
    4x, status
  • shiftedd\n", pid, pid2, status, statusgtgt8)

9
Fork and IO example
  • coosawhatcheegt more forkIO.c
  • include ltstdio.hgt
  • include ltfcntl.hgt
  • include ltsys/types.hgt
  • main()
  • int fd
  • pid_t pid / the return value from fork /
  • char buf15 / the buffer /
  • char before,child,parent, common
  • before "Before the fork\n"
  • child "child after the fork\n"
  • parent "parent after the fork\n"
  • common "common code after the fork\n"
  • if((fd open("junk",O_WRONLYO_CREATO_TRUNC,
    0777)) NULL)
  • fatal("ForkIO Test Open of junk Failed")

write(fd, before, strlen(before))
if((pid fork()) lt 0) fatal("ForkIO Test
fork failed") else if (pid 0)
write(fd, child, strlen(child)) else
write(fd, parent, strlen(parent))
sleep(1) write(fd, common, strlen(common))
fatal(char msg) fprintf(stderr,"Fatal Error
s", msg) exit(1)
10
Fork example forkstdio.c
  • include ltstdio.hgt
  • include ltsys/types.hgt
  • main()
  • FILE fp
  • pid_t pid / the return value from fork /
  • char buf150 / the buffer /
  • if((fp fopen("junk","w")) NULL)
  • fatal("ForkStdio Test Open of junk
    Failed")
  • setvbuf(fp, buf, _IOFBF, 20)
  • fprintf(fp, "Before the fork")

if((pid fork()) lt 0) fatal("ForkStdio
Test fork failed") else if (pid 0)
fprintf(fp, "child after the fork")
sleep(1) else fprintf(fp, "PARENT
AFTER THE FORK") sleep(1)
sleep(1) fprintf(fp, "Common Code after the
fork, pid valu\n",(unsigned short) pid)
11
Exec Family
  • int execl(const char path, const char arg0,
    ..., const char argn, char /NULL/)
  • int execv(const char path, char const argv)
  • int execle(const char path, const char arg0,
    ..., const char argn, char /NULL/, char
    const envp)
  • int execve(const char path, char const argv,
    char const envp)
  • int execlp(const char file, const char arg0,
    ..., const char argn, char /NULL/)
  • int execvp(const char file, char const argv)

12
exec_vi.c
  • / a simple process execing vi, the editor /
  • include ltstdio.hgt
  • main()
  • int p
  • if ((pfork()) 0)
  • execlp("vi","vi",NULL)
  • fprintf(stderr,"EXEC FAILED\n")
  • sleep (5)

13
execExamples
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com