Lecture 7 Unix Processes - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Lecture 7 Unix Processes

Description:

Time functions. September 6, 2005. CSCE 510 Systems Programming. 2. CSCE 510 Fall 2005 ... setenv-does not remove old entry if overwrite = 0 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 22
Provided by: mantonm5
Category:

less

Transcript and Presenter's Notes

Title: Lecture 7 Unix Processes


1
Lecture 7Unix Processes
CSCE 510 Systems Programming
  • Topics
  • Tsearch revisited
  • Gcc
  • Ar implementation
  • make
  • Time functions

September 6, 2005
2
  • Last Time
  • File times
  • Ar archive formats
  • Tsearch (Example tsearch.ex.c)
  • Ar implementation
  • Last Times Slides that we did not get to
  • Make (briefly today)
  • Time functions (Example time.c)
  • Today
  • Processes
  • Gnu Debugger

3
Time.c
  • include lttime.hgt
  • include ltstdio.hgt
  • main()
  • time_t now
  • struct tm bdtime, bdt_ptr
  • char buf1024
  • now time(NULL)
  • printf("the number of seconds since the Epoch
    is ld\n", now)
  • printf("the current time is s\n",
    ctime(now))
  • bdt_ptr localtime(now)
  • strftime(buf, 1024, "a b d H m j y u
    Z", bdt_ptr)
  • printf("s\n", buf)

4
User and Group IDs (UIDs, GIDs)
  • /etc/password
  • /etc/group
  • root user ID 0 ( superuser)
  • man -k userid
  • man -k groupid
  • man -k group grep name

5
Process IDs
  • Process IDs unique identifier that identifies
    the process
  • start low and get larger as machine stays up
    longer
  • Start over on reboot
  • All processes except for one or two are created
    by fork (vfork) system call
  • pid
  • ps
  • fixed PIDs
  • process 0 - scheduler
  • process 1 - init
  • process 2 -

6
Creating Processes
  • fork

7
Process Terminology
  • parent, child
  • orphan
  • exit status of a child check by parent using
    system call wait
  • zombie

8
PID syscalls
  • PID syscalls
  • getpid()
  • getppid()

9
Group ID Functions
  • Group IDs getgid(), getpgid()
  • supplementary groups
  • setgroups(int ngroups, int gidset)
  • int getgroups(int gidsetsize, int grouplist)

10
  • Effective, real, saved UID
  • int setuid(uid_t euid)
  • set uid,gid on exec

11
Memory layout of Unix Processes
  • arguments to main
  • environment
  • stack
  • heap
  • bss -
  • initialized data
  • text segment

12
Dumping pointers
  • dumpaddr(char label, void address)
    fprintf(stdout,"p s\n", address, label)
  • dumpaddr("Global addresses global", global)

13
Example printargs.c
  • / This example shows how to access command line
    arguments /
  • include main(int argc,char argv)
  • int i
  • for(i0 i lt argc i)
  • printf("argvd""s""\n", i, argvi)

14
The Environment
  • Environment
  • argv style list of strings of the form
    "namevalue"
  • extern char environ

15
Example printenv.c
  • / This example shows how to access environment
    variables /
  • include extern char environ
  • main()
  • char p
  • for(penviron p ! NULL p)
  • fprintf(stderr,"s\n", p)

16
Environment manipulation functions
  • char getenv(name)
  • setenv(name, value, overwrite)
  • void unsetenv(name)
  • int putenv (string)

17
Notes on Environment Functions
  • putenv- removes old entry if it exists
  • setenv-does not remove old entry if overwrite 0
  • What happens when we try to stuff in a longer new
    value?
  • What happens when we try to add a new name?

18
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 /

19
  • 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)

20
  • vfork
  • wait4
  • Exec family

21
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com