SlideShare una empresa de Scribd logo
1 de 71
   The C Compilation Model
   We will briefly highlight key features of the C
    Compilation model here.
   General Format cc sourcefile –options output
   The Preprocessor
    The Preprocessor accepts source code as
    input and is responsible for removing
    comments and interpreting
    special preprocessor directives denoted by #.
   C Compiler
    The C compiler translates source to assembly code.
    The source code is received from the preprocessor.
   Assembler
    The assembler creates object code. On a UNIX system
    you may see files with a .o suffix (.OBJ on MSDOS) to
    indicate object code files.
   Link Editor
    If a source file references library functions or
    functions defined in other source files the link
    editor combines these functions (with main()) to
    create an executable file.
    External Variable references resolved here also.
 -c
   Compile file.
 -llibraryname
   Link with object libraries.
   Probably the most commonly used library is
  the math library ( math.h). You must link in
  this library explicitly if you wish to use the
  maths functions (note do note forget
  to #include <math.h> header file),
  for example: cc calc.c -o calc -lm
 -Ipathname
   Add pathname to the list of directories in
  which to search for #include files with relative
  filenames (not beginning with slash /).
  cc prog.c -I/home/myname/myheaders
 -o
   Generation of object files.
   Pointers.
   Structures.
   Type Modifiers.
   signed / unsigned variables.
   All the work in the kernel should be in terms
    of processes because they form the basic test
    argument.
   But What is a process ?!!
   Process is a single thread of execution.
   One of the numerous features provided by a
    FreeBSD kernel is a protected environment
    called User Mode.
   In User Mode, a process cannot access
    hardware or protected system variables.
     -- If a process attempts to access protected
    memory, the process is terminated.
    A process running in memory consists of
     five segments: text, initialized data,
     uninitialized data, stack and heap.


                                • Machine Instructions
          Text



                               • Preinitialized variables
     Initialized Data



                         • Data that is not initialized (bss)
    Uninitialized Data



                         • used to pass parameters between functions
          Stack            and to contain local variables and function
                                        return addresses

                         • operating-system-provided area used for
          Heap                    dynamic memory allocation
   ip.h -> set of all IPs in the system.
   ip_var.h -> set of IP Data Structures which
    kernel use.
   When a process is created a complete copy of
    the original process, known as the parent, is
    created. The newly created process is called the
    child.
   Then, child process is scheduled to execute.
   The processes can only be differentiated by the
    process identifier (found in pid_t structure).
   The fork System Call.
   A new process is created when a running
    process invokes the fork system call.
    The fork system call is the only way to create
    a new process.
•#include <sys/types.h>

   Calling         •#include <unistd.h>
  Procedure        •pid_t fork(void);




                  •Fork returns the process identifier (PID) of the child to the parent process.
                  •It returns 0 to the newly created child process.
Return Values




                  •process group ID, access groups, working directory, root directory, control
Inherited Files
                   terminal, resources, interval timers, resource limits, file mode mask, and
  from Parent
                   signal mask.
   Process to
 Child Process
   execve System Call
•#include <unistd.h>
   Calling       •int execve(const char *path, char *const argv[], char *const envp[]);
  Procedure




                •path is the path and filename of the new program to execute
                •argv contains a pointer to the argument string to pass to the program.
 Parameters
                •envp contains a list of environment variables.
  Definition




                •The execve system call returns –1 on error.
                •The execve system call has numerous wrappers in the Standard C Library,
Return Values
                 known as the exec family of functions
 exit system call.
 There is another case occures unintentionally
by receiving a signal from another process.

    Note: If the parent process does not receive
    the notification, the child process becomes a
    zombie. The child will remain a zombie until
    the parent retrieves the child’s exit status.
• #include <unistd.h>
   Calling       • void _exit(int status);
  Procedure




                •_exit system call will cause the SIGCHLD signal to be thrown to the
                 parent process
Return Values




                • The _exit system call will never return.
                • The _exit system call is more commonly called by the Standard C
   Notes
                 library function exit.
   wait System Call.
   The wait system call allows a parent process
    to check to see if termination information is
    available for a child process.
   wait will suspend execution until the child
    process returns.
• #include <sys/types.h>
                     • #include <sys/wait.h>
Calling Procedure    • pid_t wait(int *status);




                    • On success, the child process identifier (PID) is returned to
                      the parent process,
                    • –1 is returned if there is an error.
Return Values       • The child’s exit status is returned by the status parameter..
int main(int argc, char **argv)
{
   pid_t pid;
   int status;
   if ((pid = fork()) > 0)
  {
     printf(“%d: waiting for the childn”, pid);
     wait(&status);
     printf(“%d: child status = %dn”, pid, status);
  }
   else if (pid == 0)
 {
    execve(“/bin/date”, NULL, NULL);
  }
exit(0);
}
# ./process
542: waiting for the child
Mon Jan 7 19:54:46 EST 2002
542: child status = 0
   Every process is created with a unique ID called
    its process identifier, or PID.
   In addition to its own PID, every process
    contains its parent process ID, or PPID.
   A listing of processes, their PIDs, and PPIDs
    may be obtained by using the ps -aj command.
 A process can retrieve its PID and PPID by
  calling the getpid and getppid functions,
  respectively.
 #include <sys/types.h>
#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);
 Q. What is the parent of proc0 ?!
   A process is assigned two users on creation,
    the real user and the effective user.
   Every process has a user identifier, UID, and a
    group identifier, GID. The UID and GID of a
    process are the username and group of the
    user that invoked the program.
   UIDs are mapped to user names in
    /etc/passwd.
   GIDs are mapped to group names in the
    /etc/group file.
   A process can retrieve its user ID and group
    ID by calling the getuid and getgid system
    calls.
#include <unistd.h>
#include <sys/types.h>
uid_t getuid(void);
uid_t getgid(void);
   When a process is created, it is assigned an
    effective user ID and an effective group ID.
   Under most circumstances, the effective ID
    and the real ID are the same.
   It is possible for a program to be configured
    so that it executes as a different user or
    group.
   the passwd utility needs root user access so it
    has permission to edit the /etc/passwd file.
   The passwd utility is configured so it
    executes with its effective user ID as root and
    gives passwd the correct permission to edit
    the /etc/passwd file
• #include <unistd.h>
                 • #include <sys/types.h>
   Calling       • uid_t geteuid(void);
  Procedure
                 • uid_t getegid(void);




                • The geteuid and getegid system calls always succeed.
Return Values




                • return the effective group and effective user.
Functionality
• #include <unistd.h>
                 • #include <sys/types.h>
   Calling       • uid_t seteuid(void);
  Procedure
                 • uid_t setguid(void);




                • The seteuid and setegid system call return 0 on success and –1 on
                  error.
Return Values




                • A process may be able to change the effective user ID or effective
                  group ID by invoking the seteuid and setegid system calls.
                • The seteuid and setegid system calls can set the effective user ID
Functionality
                  and effective group ID
int main(int argc, char **argv)
  {
    printf(“UID = %dn”, getuid());
    printf(“GID = %dn”, getgid());
    printf(“EUID = %dn”, geteuid());
    printf(“EGID = %dn”, getegid());
    exit(0);
}
# ./ids
UID = 1001
GID = 1001
EUID = 1001
EGID = 1001
                       ###########
 Here is my logon name in the /etc/password file; column
  three contains my UID.
farrag:*:1001:1001:Mohammed Farrag:/home/farrag:/bin/sh
 Here is my login group entry from the /etc/group file;
  column three contains my GID.
farrag:*:1001:
   A process group consists of a group of
    related processes. In addition to the PID and
    PPID, a process contains the process group
    ID, the PGID.
   Each process group has a unique process
    group identifier.
   Every process group is able to have a single
    process group leader; a process group leader
    is denoted by the process group ID being the
    same as the PID.
• #include <unistd.h>
  Calling        • int setpgid(pid_t pid, pid_t pgrp);
 Procedure       • pid_t getpgid(pid_t pid);


                • pgrp : Process Group.
Parameters • pid: Process ID.
 Definition




                • set and get its PGID
Functionality



                • A process is only able to set the process group of itself and its
                  children processes.
 Notes          • One use of process groups is to send signals to a group of processes.
   File Descriptors.
   Permissions
   Current Working Directory
   A file descriptor is a low-level interface used
    to access an IO interface.
   File descriptors are implemented as an int.
   Every process contains a list of open file
    descriptors.
   A child process inherits any open file
    descriptors from the parent.
   Every process contains a default set of file
    permissions called file creation mode mask.
   The file creation mode mask is 9 bits that
    represent read, write and execute
    permissions for the file owner, file group, and
    everybody else.
   A processes file creation mode mask is
    modified by the umask system call.
• #include <sys/stat.h>
 Calling
Procedure
          • mode_t umask(mode_t numask);



Parameters
                • mode_t is file creation mode structure.
 Definition



                • sets the process file creation mask to the value contained in umask -> 9
Functionality     bits (user, group and other -> 3 bits each(r,w,e)).




 Return         • The previous value of the file creation mask is returned to the caller.
 Values
   Every process contains a current working
    directory, which is the directory where the
    process was started.
• #include <unistd.h>
 Calling
Procedure
          • int chdir(const char *path);


                • The path argument contains the path to be used as the current working
Parameters        directory.
 Definition




                • A process may change its current working directory
Functionality




 Return         • The chdir system call returns 0 on success and –1 on error.
 Values
   A process inherits the resource limits from its
    parent process. Resources may be read or
    modified by the setrlimit and getrlimit system
    calls.
• #include <sys/types.h>
                • #include <sys/time.h>
 Calling        • #include <sys/resource.h>
Procedure
                • int getrlimit(int resource, struct rlimit *rlp);
                • int setrlimit(int resource, const struct rlimit *rlp);

                • take a resource parameter that specifies the resource to be read or
                  written; a list of resources is contained in
Parameters
 Definition       /usr/include/sys/resource.h. The second argument is a struct rlimit
                  pointer.

Functionality
                • read and modify process resource limits.


Return • The setrlimit and getrlimit system calls return 0 on success and –1 on error.
Values
   struct rlimit {
   rlim_t rlim_cur; /* current (soft) limit */
   rlim_t rlim_max; /* maximum value for
    rlim_cur */
   };
   A set of process groups can be collected into
    a session, a set of processes that are
    associated with a controlling terminal.
   Sessions are used to group a user login shell
    and the process it creates or to create an
    isolated environment for a daemon process.
• #include <unistd.h>
                     • pid_t setsid(void);
Calling Procedure




                    • The setsid() function creates a new session.
                    • The calling process is the session leader of the new
                      session, is the process group leader of a new process
                      group, and has no controlling terminal.
 Functionality      • The calling process is the only process in either the
                      session or the process group.
   A session may have a controlling terminal;
    this is the device used to logon.
   The session leader that initiated the
    connection of the controlling terminal is
    considered the controlling process.
   The controlling terminal is established for us
    when we login.
   Priority
   State
   Execution time is made available to a process
    according to its process priority.
   Priorities range from 0 through 127.
   Process priorities are defined in
    /usr/include/sys/param.h.
   The lower the process priority, the more
    favorable scheduling priority it receives.
• #include <sys/time.h>
                 • #include <sys/resource.h>
   Calling       • int getpriority(int which, int who);
  Procedure
                 • int setpriority(int which, int who, int prio);



                • The setpriority and getpriority system calls return 0 on success and –1 on
                  error.
Return Values




                • A process priority may be read and modified using the setpriority and
                  getpriority system calls.
Functionality
   the who argument is to be interpreted as a
    process ID, a process group ID, or an
    effective user ID, respectively.
   A 0 value for the who argument specifies the
    current process, process group, or user
   The setpriority() function shall set the nice value
    of a process, process group, or user to value+
    {NZERO} (in the range [ {NZERO},{NZERO} -1]).
   A process is in one of five states at any time.
   The process state is used internally by the
    FreeBSD kernel to organize processes.
   Signals are mechanisms to notify a process
    that a system event has occurred.
   Every process contains a table that defines an
    associated action to handle a signal that is
    delivered to a process.
   at process creation, all signals contain a
    default action.
   A signal is handled in one of three ways: it is
    ignored, caught, or handled
   by the default action set by the kernel.
   A user can define a function that is invoked
    when a process receives a signal; this is called
    a signal handler.
   The signal handler is said to catch the signal.
    Signals are defined in
    /usr/include/sys/signal.h.
   It is important to note that two signals,
    SIGSTOP and SIGKILL, cannot be caught and
    will terminate a process.
   The signal function is used to specify a user-
    defined signal handler for a specific signal.
    The signal function is a wrapper for the
    sigaction system call.
• #include <signal.h>
 Calling       • void (*sig_t) (int)
Procedure      • sig_t signal(int sig, sig_t func);



               • The sig parameter specifies which signal the signal handler will catch.
                 The func parameter allows a user to specify a user-defined signal
                 handler. The
               • default action of the signal may be reset, by specifying SIG_DFL as the
Parameters
 Definition      signal handler. A signal may be ignored, by specifying SIG_IGN as the
                 signal handler.
               • Ignoring a signal causes subsequent instances of the signal to be
                 ignored and pending instances to be discarded.


              • If successful, signal returns the previous action;
 Return       • otherwise, SIG_ERR is returned and the global variable errno is set to indicate
 Values         the error.
• The kill system call sends a signal to a process or group process group.
Functionality



                • #include <sys/types.h>
 Calling        • #include <signal.h>
Procedure
                • int kill(pid_t pid, int sig);

                •The pid parameter is the process ID to send the signal. A process ID greater than zero is
                 sent to that specific process
                •. A process ID of zero sends the signal to all the members of the process group of the
                 sending process.
Parameters
Definition      •A process ID of –1 sends the signal to all processes if the super user;
                •otherwise, it is sent to all processes with the same user ID as the sending process.
                •Sig is the signal sent.


                •The kill() function returns the value 0 if successful;
Return •otherwise the value –1 is returned and the global variable errno is set to indicate the
Values  error.
   A daemon is a special process that runs in the
    background, usually providing a service.
   Daemon processes are usually started at
    system boot time and remain running until
    the system is halted.
   All daemons have some common process
    attributes.
   This can be done using A Skeleton Daemon
    Function called handle_sigcld Function.
   by installing the handle_sigcld signal handler
    to catch the SIGCLD signal when a child exits.
void handle_sigcld()           Note that the handle_sigcld
                               function invokes the wait3
{                              system call as NOHANG. This is
int pid;                       so the parent continues
                               execution and does not block
int status;                    waiting for a child to exit. This
                               allows the parent to continue
while ((pid = wait3(&status,   responding to requests or to
  WNOHANG, (struct rusage      perform other tasks.

  *)NULL))
> 0);
}
   WNOHANG
        which means to return immediately if no child is there to be waited for.
   WUNTRACED
        which means to also return for children which are stopped, and whose status
    has not been reported.
   WIFEXITED (status)
        is non-zero if the child exited normally.
   WEXITSTATUS (status)
         evaluates to the least significant eight bits of the return code of the child
    which terminated, which may have been set as the argument to a call to exit() or
    as the argument for a return statement in the main program.
   WIFSIGNALED (status)
        returns true if the child process exited because of a signal which was not
    caught.
   WTERMSIG (status)
         returns the number of the signal that caused the child process to terminate.
   If rusage is not NULL, the struct rusage as defined in <sys/resource.h> it points to
    will be filled with accounting information.
void init_daemon()
{
int childpid = 0;
int fd = 0;
struct rlimit max_files;
/*
** create a child process
*/
if ((childpid = fork()) < 0)
{
/* handle the error condition */
exit(-1);
}
else if (childpid > 0)
{
/* this is the parent, we may successfully exit */
exit(0);
}
/* now executing as the child process */
/* become the session leader */
setsid();

/*
** close all open file descriptors, need to get the maximum
** number of open files from getrlimit.
*/
bzero(&max_files, sizeof(struct rlimit));
getrlimit(RLIMIT_NOFILE, &max_files);
for (fd = 0; fd < max_files.rlim_max; fd++)
{
close(fd);
}
/*
** the current working directory is held open by the
** kernel for the life of a process so the file system
** cannot be unmounted, reset the current working
** directory to root.
*/
chdir(“/”);
/*
** a process inherits file access permissions from the
** process which created it, reset the user access mask
*/
umask(0);
/*
** we don’t care about the exit status but we need to
** cleanup after each process that handles a request so
** the system isn’t flooded with zombie processes
*/
signal(SIGCHLD, handle_sigcld);
}
Lecture2   process structure and programming
Lecture2   process structure and programming

Más contenido relacionado

La actualidad más candente

Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linuxLiran Ben Haim
 
Kernel init
Kernel initKernel init
Kernel initgowell
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelDivye Kapoor
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityOMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityAndrew Case
 
De-Anonymizing Live CDs through Physical Memory Analysis
De-Anonymizing Live CDs through Physical Memory AnalysisDe-Anonymizing Live CDs through Physical Memory Analysis
De-Anonymizing Live CDs through Physical Memory AnalysisAndrew Case
 
Introduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversIntroduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversRajKumar Rampelli
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device DriverGary Yeh
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKBLinux Kernel Module - For NLKB
Linux Kernel Module - For NLKBshimosawa
 
Hunting Mac Malware with Memory Forensics
Hunting Mac Malware with Memory ForensicsHunting Mac Malware with Memory Forensics
Hunting Mac Malware with Memory ForensicsAndrew Case
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsDivye Kapoor
 

La actualidad más candente (20)

Linux IO
Linux IOLinux IO
Linux IO
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linux
 
Kernel init
Kernel initKernel init
Kernel init
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Ch1 linux basics
Ch1 linux basicsCh1 linux basics
Ch1 linux basics
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityOMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
 
De-Anonymizing Live CDs through Physical Memory Analysis
De-Anonymizing Live CDs through Physical Memory AnalysisDe-Anonymizing Live CDs through Physical Memory Analysis
De-Anonymizing Live CDs through Physical Memory Analysis
 
Introduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversIntroduction to Kernel and Device Drivers
Introduction to Kernel and Device Drivers
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKBLinux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
 
Linux Kernel I/O Schedulers
Linux Kernel I/O SchedulersLinux Kernel I/O Schedulers
Linux Kernel I/O Schedulers
 
Hunting Mac Malware with Memory Forensics
Hunting Mac Malware with Memory ForensicsHunting Mac Malware with Memory Forensics
Hunting Mac Malware with Memory Forensics
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOs
 
Linux watchdog timer
Linux watchdog timerLinux watchdog timer
Linux watchdog timer
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
unixtoolbox
unixtoolboxunixtoolbox
unixtoolbox
 
淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道 淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道
 
101 1.2 boot the system
101 1.2 boot the system101 1.2 boot the system
101 1.2 boot the system
 
Introduction to UNIX
Introduction to UNIXIntroduction to UNIX
Introduction to UNIX
 

Destacado

Cymphonix Delivers Visibility and Control of Internet Content
Cymphonix Delivers Visibility and Control of Internet ContentCymphonix Delivers Visibility and Control of Internet Content
Cymphonix Delivers Visibility and Control of Internet ContentJulie Tangen
 
Java Eye Architecture
Java Eye ArchitectureJava Eye Architecture
Java Eye ArchitectureRobbin Fan
 
Wiki Technology By It Rocks
Wiki Technology By It RocksWiki Technology By It Rocks
Wiki Technology By It Rocksnaveenv
 
Mobile Web Rock
Mobile Web RockMobile Web Rock
Mobile Web RockIdo Green
 
Collaborative Assessment: Working Together Toward Institutional Change
Collaborative Assessment: Working Together Toward Institutional ChangeCollaborative Assessment: Working Together Toward Institutional Change
Collaborative Assessment: Working Together Toward Institutional ChangeElizabeth Nesius
 
The High School Connection: Bridging the Gap Between High School and College
The High School Connection: Bridging the Gap Between High School and CollegeThe High School Connection: Bridging the Gap Between High School and College
The High School Connection: Bridging the Gap Between High School and CollegeElizabeth Nesius
 
Oracle Exec Summary 7000 Unified Storage
Oracle Exec Summary 7000 Unified StorageOracle Exec Summary 7000 Unified Storage
Oracle Exec Summary 7000 Unified StorageDavid R. Klauser
 
Web并发模型粗浅探讨
Web并发模型粗浅探讨Web并发模型粗浅探讨
Web并发模型粗浅探讨Robbin Fan
 
Axiologix Company Presentation Jan 2011
Axiologix Company Presentation Jan 2011Axiologix Company Presentation Jan 2011
Axiologix Company Presentation Jan 2011InboundMarketingPR.com
 
Dissertation on MF
Dissertation on MFDissertation on MF
Dissertation on MFPIYUSH JAIN
 
Dec 06, Sermon Am
Dec 06, Sermon AmDec 06, Sermon Am
Dec 06, Sermon AmGeo Acts
 
Will you help kill your 30 year Mortgage?
Will you help kill your 30 year Mortgage?Will you help kill your 30 year Mortgage?
Will you help kill your 30 year Mortgage?payitearly
 
Cymphonix Launches iPhone App and New Version of Network Composer Software
Cymphonix Launches iPhone App and New Version of Network Composer SoftwareCymphonix Launches iPhone App and New Version of Network Composer Software
Cymphonix Launches iPhone App and New Version of Network Composer SoftwareJulie Tangen
 
Europa Del Settecento
Europa Del SettecentoEuropa Del Settecento
Europa Del Settecentomapaa
 

Destacado (20)

Cymphonix Delivers Visibility and Control of Internet Content
Cymphonix Delivers Visibility and Control of Internet ContentCymphonix Delivers Visibility and Control of Internet Content
Cymphonix Delivers Visibility and Control of Internet Content
 
Java Eye Architecture
Java Eye ArchitectureJava Eye Architecture
Java Eye Architecture
 
Wiki Technology By It Rocks
Wiki Technology By It RocksWiki Technology By It Rocks
Wiki Technology By It Rocks
 
VANABBETOTVESSEM
VANABBETOTVESSEMVANABBETOTVESSEM
VANABBETOTVESSEM
 
Why Ruby?
Why Ruby?Why Ruby?
Why Ruby?
 
Mobile Web Rock
Mobile Web RockMobile Web Rock
Mobile Web Rock
 
Collaborative Assessment: Working Together Toward Institutional Change
Collaborative Assessment: Working Together Toward Institutional ChangeCollaborative Assessment: Working Together Toward Institutional Change
Collaborative Assessment: Working Together Toward Institutional Change
 
The High School Connection: Bridging the Gap Between High School and College
The High School Connection: Bridging the Gap Between High School and CollegeThe High School Connection: Bridging the Gap Between High School and College
The High School Connection: Bridging the Gap Between High School and College
 
Oracle Exec Summary 7000 Unified Storage
Oracle Exec Summary 7000 Unified StorageOracle Exec Summary 7000 Unified Storage
Oracle Exec Summary 7000 Unified Storage
 
Web并发模型粗浅探讨
Web并发模型粗浅探讨Web并发模型粗浅探讨
Web并发模型粗浅探讨
 
Axiologix Company Presentation Jan 2011
Axiologix Company Presentation Jan 2011Axiologix Company Presentation Jan 2011
Axiologix Company Presentation Jan 2011
 
Dissertation on MF
Dissertation on MFDissertation on MF
Dissertation on MF
 
5 things MySql
5 things MySql5 things MySql
5 things MySql
 
Aptso cosechas 2010
Aptso cosechas 2010Aptso cosechas 2010
Aptso cosechas 2010
 
Dec 06, Sermon Am
Dec 06, Sermon AmDec 06, Sermon Am
Dec 06, Sermon Am
 
Will you help kill your 30 year Mortgage?
Will you help kill your 30 year Mortgage?Will you help kill your 30 year Mortgage?
Will you help kill your 30 year Mortgage?
 
Europe
EuropeEurope
Europe
 
Cymphonix Launches iPhone App and New Version of Network Composer Software
Cymphonix Launches iPhone App and New Version of Network Composer SoftwareCymphonix Launches iPhone App and New Version of Network Composer Software
Cymphonix Launches iPhone App and New Version of Network Composer Software
 
èDip 4rt eso
èDip 4rt esoèDip 4rt eso
èDip 4rt eso
 
Europa Del Settecento
Europa Del SettecentoEuropa Del Settecento
Europa Del Settecento
 

Similar a Lecture2 process structure and programming

Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesHelpWithAssignment.com
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsashukiller7
 
System Calls - Introduction
System Calls - IntroductionSystem Calls - Introduction
System Calls - IntroductionTo Sum It Up
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device DriversNEEVEE Technologies
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptxvnwzympx
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.pptSIDDHARTHANANDCSE202
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104swena_gupta
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104swena_gupta
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104swena_gupta
 
UNIX_Process Control_Module3.pptx
UNIX_Process Control_Module3.pptxUNIX_Process Control_Module3.pptx
UNIX_Process Control_Module3.pptxraunakkumar290158
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxSoumen Santra
 
Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.CocoaHeads France
 
Advanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementAdvanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementVeejeya Kumbhar
 

Similar a Lecture2 process structure and programming (20)

Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - Processes
 
Os lab final
Os lab finalOs lab final
Os lab final
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
 
System Calls - Introduction
System Calls - IntroductionSystem Calls - Introduction
System Calls - Introduction
 
Unix kernal
Unix kernalUnix kernal
Unix kernal
 
Lect3 process
Lect3 processLect3 process
Lect3 process
 
process creation OS
process creation OSprocess creation OS
process creation OS
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
Process management
Process managementProcess management
Process management
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
 
process group.
process group.process group.
process group.
 
UNIX_Process Control_Module3.pptx
UNIX_Process Control_Module3.pptxUNIX_Process Control_Module3.pptx
UNIX_Process Control_Module3.pptx
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with Linux
 
Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.
 
Advanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementAdvanced Operating Systems......Process Management
Advanced Operating Systems......Process Management
 

Más de Mohammed Farrag

Resume for Mohamed Farag
Resume for Mohamed FaragResume for Mohamed Farag
Resume for Mohamed FaragMohammed Farrag
 
Mum Article Recognition for Mohamed Farag
Mum Article Recognition for Mohamed FaragMum Article Recognition for Mohamed Farag
Mum Article Recognition for Mohamed FaragMohammed Farrag
 
Artificial Intelligence Programming in Art by Mohamed Farag
Artificial Intelligence Programming in Art by Mohamed FaragArtificial Intelligence Programming in Art by Mohamed Farag
Artificial Intelligence Programming in Art by Mohamed FaragMohammed Farrag
 
The practices, challenges and opportunities of board composition and leadersh...
The practices, challenges and opportunities of board composition and leadersh...The practices, challenges and opportunities of board composition and leadersh...
The practices, challenges and opportunities of board composition and leadersh...Mohammed Farrag
 
Confirmation letter info tech 2013(1)
Confirmation letter info tech 2013(1)Confirmation letter info tech 2013(1)
Confirmation letter info tech 2013(1)Mohammed Farrag
 

Más de Mohammed Farrag (10)

Resume for Mohamed Farag
Resume for Mohamed FaragResume for Mohamed Farag
Resume for Mohamed Farag
 
Mum Article Recognition for Mohamed Farag
Mum Article Recognition for Mohamed FaragMum Article Recognition for Mohamed Farag
Mum Article Recognition for Mohamed Farag
 
Create 2015 Event
Create 2015 EventCreate 2015 Event
Create 2015 Event
 
Artificial Intelligence Programming in Art by Mohamed Farag
Artificial Intelligence Programming in Art by Mohamed FaragArtificial Intelligence Programming in Art by Mohamed Farag
Artificial Intelligence Programming in Art by Mohamed Farag
 
The practices, challenges and opportunities of board composition and leadersh...
The practices, challenges and opportunities of board composition and leadersh...The practices, challenges and opportunities of board composition and leadersh...
The practices, challenges and opportunities of board composition and leadersh...
 
Confirmation letter info tech 2013(1)
Confirmation letter info tech 2013(1)Confirmation letter info tech 2013(1)
Confirmation letter info tech 2013(1)
 
Google APPs and APIs
Google APPs and APIsGoogle APPs and APIs
Google APPs and APIs
 
FreeBSD Handbook
FreeBSD HandbookFreeBSD Handbook
FreeBSD Handbook
 
Master resume
Master resumeMaster resume
Master resume
 
Mohammed Farrag Resume
Mohammed Farrag ResumeMohammed Farrag Resume
Mohammed Farrag Resume
 

Último

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Último (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

Lecture2 process structure and programming

  • 1.
  • 2. The C Compilation Model  We will briefly highlight key features of the C Compilation model here.  General Format cc sourcefile –options output
  • 3. The Preprocessor The Preprocessor accepts source code as input and is responsible for removing comments and interpreting special preprocessor directives denoted by #.
  • 4. C Compiler The C compiler translates source to assembly code. The source code is received from the preprocessor.  Assembler The assembler creates object code. On a UNIX system you may see files with a .o suffix (.OBJ on MSDOS) to indicate object code files.  Link Editor If a source file references library functions or functions defined in other source files the link editor combines these functions (with main()) to create an executable file. External Variable references resolved here also.
  • 5.  -c Compile file.  -llibraryname Link with object libraries. Probably the most commonly used library is the math library ( math.h). You must link in this library explicitly if you wish to use the maths functions (note do note forget to #include <math.h> header file), for example: cc calc.c -o calc -lm
  • 6.  -Ipathname Add pathname to the list of directories in which to search for #include files with relative filenames (not beginning with slash /). cc prog.c -I/home/myname/myheaders  -o Generation of object files.
  • 7. Pointers.  Structures.  Type Modifiers.  signed / unsigned variables.
  • 8. All the work in the kernel should be in terms of processes because they form the basic test argument.  But What is a process ?!!
  • 9. Process is a single thread of execution.  One of the numerous features provided by a FreeBSD kernel is a protected environment called User Mode.  In User Mode, a process cannot access hardware or protected system variables. -- If a process attempts to access protected memory, the process is terminated.
  • 10. A process running in memory consists of five segments: text, initialized data, uninitialized data, stack and heap. • Machine Instructions Text • Preinitialized variables Initialized Data • Data that is not initialized (bss) Uninitialized Data • used to pass parameters between functions Stack and to contain local variables and function return addresses • operating-system-provided area used for Heap dynamic memory allocation
  • 11. ip.h -> set of all IPs in the system.  ip_var.h -> set of IP Data Structures which kernel use.
  • 12.
  • 13.
  • 14. When a process is created a complete copy of the original process, known as the parent, is created. The newly created process is called the child.  Then, child process is scheduled to execute.  The processes can only be differentiated by the process identifier (found in pid_t structure).
  • 15. The fork System Call.  A new process is created when a running process invokes the fork system call.  The fork system call is the only way to create a new process.
  • 16. •#include <sys/types.h> Calling •#include <unistd.h> Procedure •pid_t fork(void); •Fork returns the process identifier (PID) of the child to the parent process. •It returns 0 to the newly created child process. Return Values •process group ID, access groups, working directory, root directory, control Inherited Files terminal, resources, interval timers, resource limits, file mode mask, and from Parent signal mask. Process to Child Process
  • 17. execve System Call
  • 18. •#include <unistd.h> Calling •int execve(const char *path, char *const argv[], char *const envp[]); Procedure •path is the path and filename of the new program to execute •argv contains a pointer to the argument string to pass to the program. Parameters •envp contains a list of environment variables. Definition •The execve system call returns –1 on error. •The execve system call has numerous wrappers in the Standard C Library, Return Values known as the exec family of functions
  • 19.  exit system call.  There is another case occures unintentionally by receiving a signal from another process.  Note: If the parent process does not receive the notification, the child process becomes a zombie. The child will remain a zombie until the parent retrieves the child’s exit status.
  • 20. • #include <unistd.h> Calling • void _exit(int status); Procedure •_exit system call will cause the SIGCHLD signal to be thrown to the parent process Return Values • The _exit system call will never return. • The _exit system call is more commonly called by the Standard C Notes library function exit.
  • 21. wait System Call.  The wait system call allows a parent process to check to see if termination information is available for a child process.  wait will suspend execution until the child process returns.
  • 22. • #include <sys/types.h> • #include <sys/wait.h> Calling Procedure • pid_t wait(int *status); • On success, the child process identifier (PID) is returned to the parent process, • –1 is returned if there is an error. Return Values • The child’s exit status is returned by the status parameter..
  • 23. int main(int argc, char **argv) { pid_t pid; int status; if ((pid = fork()) > 0) { printf(“%d: waiting for the childn”, pid); wait(&status); printf(“%d: child status = %dn”, pid, status); } else if (pid == 0) { execve(“/bin/date”, NULL, NULL); } exit(0); }
  • 24. # ./process 542: waiting for the child Mon Jan 7 19:54:46 EST 2002 542: child status = 0
  • 25. Every process is created with a unique ID called its process identifier, or PID.  In addition to its own PID, every process contains its parent process ID, or PPID.  A listing of processes, their PIDs, and PPIDs may be obtained by using the ps -aj command.
  • 26.
  • 27.  A process can retrieve its PID and PPID by calling the getpid and getppid functions, respectively.  #include <sys/types.h> #include <unistd.h> pid_t getpid(void); pid_t getppid(void);  Q. What is the parent of proc0 ?!
  • 28. A process is assigned two users on creation, the real user and the effective user.
  • 29. Every process has a user identifier, UID, and a group identifier, GID. The UID and GID of a process are the username and group of the user that invoked the program.  UIDs are mapped to user names in /etc/passwd.  GIDs are mapped to group names in the /etc/group file.  A process can retrieve its user ID and group ID by calling the getuid and getgid system calls.
  • 30. #include <unistd.h> #include <sys/types.h> uid_t getuid(void); uid_t getgid(void);
  • 31. When a process is created, it is assigned an effective user ID and an effective group ID.  Under most circumstances, the effective ID and the real ID are the same.  It is possible for a program to be configured so that it executes as a different user or group.
  • 32. the passwd utility needs root user access so it has permission to edit the /etc/passwd file.  The passwd utility is configured so it executes with its effective user ID as root and gives passwd the correct permission to edit the /etc/passwd file
  • 33. • #include <unistd.h> • #include <sys/types.h> Calling • uid_t geteuid(void); Procedure • uid_t getegid(void); • The geteuid and getegid system calls always succeed. Return Values • return the effective group and effective user. Functionality
  • 34. • #include <unistd.h> • #include <sys/types.h> Calling • uid_t seteuid(void); Procedure • uid_t setguid(void); • The seteuid and setegid system call return 0 on success and –1 on error. Return Values • A process may be able to change the effective user ID or effective group ID by invoking the seteuid and setegid system calls. • The seteuid and setegid system calls can set the effective user ID Functionality and effective group ID
  • 35. int main(int argc, char **argv) { printf(“UID = %dn”, getuid()); printf(“GID = %dn”, getgid()); printf(“EUID = %dn”, geteuid()); printf(“EGID = %dn”, getegid()); exit(0); }
  • 36. # ./ids UID = 1001 GID = 1001 EUID = 1001 EGID = 1001 ###########  Here is my logon name in the /etc/password file; column three contains my UID. farrag:*:1001:1001:Mohammed Farrag:/home/farrag:/bin/sh  Here is my login group entry from the /etc/group file; column three contains my GID. farrag:*:1001:
  • 37. A process group consists of a group of related processes. In addition to the PID and PPID, a process contains the process group ID, the PGID.  Each process group has a unique process group identifier.  Every process group is able to have a single process group leader; a process group leader is denoted by the process group ID being the same as the PID.
  • 38. • #include <unistd.h> Calling • int setpgid(pid_t pid, pid_t pgrp); Procedure • pid_t getpgid(pid_t pid); • pgrp : Process Group. Parameters • pid: Process ID. Definition • set and get its PGID Functionality • A process is only able to set the process group of itself and its children processes. Notes • One use of process groups is to send signals to a group of processes.
  • 39. File Descriptors.  Permissions  Current Working Directory
  • 40. A file descriptor is a low-level interface used to access an IO interface.  File descriptors are implemented as an int.  Every process contains a list of open file descriptors.  A child process inherits any open file descriptors from the parent.
  • 41. Every process contains a default set of file permissions called file creation mode mask.  The file creation mode mask is 9 bits that represent read, write and execute permissions for the file owner, file group, and everybody else.  A processes file creation mode mask is modified by the umask system call.
  • 42. • #include <sys/stat.h> Calling Procedure • mode_t umask(mode_t numask); Parameters • mode_t is file creation mode structure. Definition • sets the process file creation mask to the value contained in umask -> 9 Functionality bits (user, group and other -> 3 bits each(r,w,e)). Return • The previous value of the file creation mask is returned to the caller. Values
  • 43. Every process contains a current working directory, which is the directory where the process was started.
  • 44. • #include <unistd.h> Calling Procedure • int chdir(const char *path); • The path argument contains the path to be used as the current working Parameters directory. Definition • A process may change its current working directory Functionality Return • The chdir system call returns 0 on success and –1 on error. Values
  • 45. A process inherits the resource limits from its parent process. Resources may be read or modified by the setrlimit and getrlimit system calls.
  • 46. • #include <sys/types.h> • #include <sys/time.h> Calling • #include <sys/resource.h> Procedure • int getrlimit(int resource, struct rlimit *rlp); • int setrlimit(int resource, const struct rlimit *rlp); • take a resource parameter that specifies the resource to be read or written; a list of resources is contained in Parameters Definition /usr/include/sys/resource.h. The second argument is a struct rlimit pointer. Functionality • read and modify process resource limits. Return • The setrlimit and getrlimit system calls return 0 on success and –1 on error. Values
  • 47. struct rlimit {  rlim_t rlim_cur; /* current (soft) limit */  rlim_t rlim_max; /* maximum value for rlim_cur */  };
  • 48. A set of process groups can be collected into a session, a set of processes that are associated with a controlling terminal.  Sessions are used to group a user login shell and the process it creates or to create an isolated environment for a daemon process.
  • 49. • #include <unistd.h> • pid_t setsid(void); Calling Procedure • The setsid() function creates a new session. • The calling process is the session leader of the new session, is the process group leader of a new process group, and has no controlling terminal. Functionality • The calling process is the only process in either the session or the process group.
  • 50. A session may have a controlling terminal; this is the device used to logon.  The session leader that initiated the connection of the controlling terminal is considered the controlling process.  The controlling terminal is established for us when we login.
  • 51. Priority  State
  • 52. Execution time is made available to a process according to its process priority.  Priorities range from 0 through 127.  Process priorities are defined in /usr/include/sys/param.h.  The lower the process priority, the more favorable scheduling priority it receives.
  • 53. • #include <sys/time.h> • #include <sys/resource.h> Calling • int getpriority(int which, int who); Procedure • int setpriority(int which, int who, int prio); • The setpriority and getpriority system calls return 0 on success and –1 on error. Return Values • A process priority may be read and modified using the setpriority and getpriority system calls. Functionality
  • 54.
  • 55. the who argument is to be interpreted as a process ID, a process group ID, or an effective user ID, respectively.  A 0 value for the who argument specifies the current process, process group, or user
  • 56. The setpriority() function shall set the nice value of a process, process group, or user to value+ {NZERO} (in the range [ {NZERO},{NZERO} -1]).
  • 57. A process is in one of five states at any time.  The process state is used internally by the FreeBSD kernel to organize processes.
  • 58. Signals are mechanisms to notify a process that a system event has occurred.  Every process contains a table that defines an associated action to handle a signal that is delivered to a process.  at process creation, all signals contain a default action.  A signal is handled in one of three ways: it is ignored, caught, or handled  by the default action set by the kernel.
  • 59. A user can define a function that is invoked when a process receives a signal; this is called a signal handler.  The signal handler is said to catch the signal. Signals are defined in /usr/include/sys/signal.h.  It is important to note that two signals, SIGSTOP and SIGKILL, cannot be caught and will terminate a process.
  • 60. The signal function is used to specify a user- defined signal handler for a specific signal.  The signal function is a wrapper for the sigaction system call.
  • 61. • #include <signal.h> Calling • void (*sig_t) (int) Procedure • sig_t signal(int sig, sig_t func); • The sig parameter specifies which signal the signal handler will catch. The func parameter allows a user to specify a user-defined signal handler. The • default action of the signal may be reset, by specifying SIG_DFL as the Parameters Definition signal handler. A signal may be ignored, by specifying SIG_IGN as the signal handler. • Ignoring a signal causes subsequent instances of the signal to be ignored and pending instances to be discarded. • If successful, signal returns the previous action; Return • otherwise, SIG_ERR is returned and the global variable errno is set to indicate Values the error.
  • 62. • The kill system call sends a signal to a process or group process group. Functionality • #include <sys/types.h> Calling • #include <signal.h> Procedure • int kill(pid_t pid, int sig); •The pid parameter is the process ID to send the signal. A process ID greater than zero is sent to that specific process •. A process ID of zero sends the signal to all the members of the process group of the sending process. Parameters Definition •A process ID of –1 sends the signal to all processes if the super user; •otherwise, it is sent to all processes with the same user ID as the sending process. •Sig is the signal sent. •The kill() function returns the value 0 if successful; Return •otherwise the value –1 is returned and the global variable errno is set to indicate the Values error.
  • 63. A daemon is a special process that runs in the background, usually providing a service.  Daemon processes are usually started at system boot time and remain running until the system is halted.  All daemons have some common process attributes.
  • 64. This can be done using A Skeleton Daemon Function called handle_sigcld Function.  by installing the handle_sigcld signal handler to catch the SIGCLD signal when a child exits.
  • 65. void handle_sigcld() Note that the handle_sigcld function invokes the wait3 { system call as NOHANG. This is int pid; so the parent continues execution and does not block int status; waiting for a child to exit. This allows the parent to continue while ((pid = wait3(&status, responding to requests or to WNOHANG, (struct rusage perform other tasks. *)NULL)) > 0); }
  • 66. WNOHANG which means to return immediately if no child is there to be waited for.  WUNTRACED which means to also return for children which are stopped, and whose status has not been reported.  WIFEXITED (status) is non-zero if the child exited normally.  WEXITSTATUS (status) evaluates to the least significant eight bits of the return code of the child which terminated, which may have been set as the argument to a call to exit() or as the argument for a return statement in the main program.  WIFSIGNALED (status) returns true if the child process exited because of a signal which was not caught.  WTERMSIG (status) returns the number of the signal that caused the child process to terminate.  If rusage is not NULL, the struct rusage as defined in <sys/resource.h> it points to will be filled with accounting information.
  • 67. void init_daemon() { int childpid = 0; int fd = 0; struct rlimit max_files; /* ** create a child process */ if ((childpid = fork()) < 0) { /* handle the error condition */ exit(-1); } else if (childpid > 0) { /* this is the parent, we may successfully exit */ exit(0); }
  • 68. /* now executing as the child process */ /* become the session leader */ setsid(); /* ** close all open file descriptors, need to get the maximum ** number of open files from getrlimit. */ bzero(&max_files, sizeof(struct rlimit)); getrlimit(RLIMIT_NOFILE, &max_files); for (fd = 0; fd < max_files.rlim_max; fd++) { close(fd); }
  • 69. /* ** the current working directory is held open by the ** kernel for the life of a process so the file system ** cannot be unmounted, reset the current working ** directory to root. */ chdir(“/”); /* ** a process inherits file access permissions from the ** process which created it, reset the user access mask */ umask(0); /* ** we don’t care about the exit status but we need to ** cleanup after each process that handles a request so ** the system isn’t flooded with zombie processes */ signal(SIGCHLD, handle_sigcld); }