Unix System Call

System Call could be called in order to obtain services from Unix-based operating system such as Linux and #macos.

The POSIX library provides several C functions, shown as the following, for a program to call system call:

  • fork (create process by duplicating the calling Process#)
  • exec (execute a new Program# and replace the current process)
    • execlp (the file will be searched in PATH environmental variable)
    • execl
    • execle
    • execvp (the file will be searched in PATH environmental variable)
    • execv
    • execve
  • wait (suspends the parent process until the child finish its execution)
  • waitpid (like wait but with specific process ID)
    • WIFEXITED returns non-zero if child terminated normally
    • WEXITSTATUS returns the exit status of the child process
    • WNOHANGS flags the process to not wait for the child
  • exit (to terminate Process#)
  • socket
  • stat (get file attributes)
  • fstat (get file attributes)
  • system (invoke commands)
  • open (open file)
  • creat (create file)
  • close (close File Descriptor#)
  • read (read information from file)
  • write (write information to file)
  • lseek (moves the file pointer)
  • fcntl (change the running file’s properties)
  • kill (send signal to process)
  • msgget (create or access to Message Queue)
  • msgctl (control operations on a message queue)
  • msgsnd (write message to the message queue)
  • msgrcv (read message from the message queue)
  • semget (create or access to Semaphore in System V)
  • semctl (control operations on a semaphore)
  • semop (perform operations on semaphore)
Links to this page
  • TTP3121 Chapter 4: Unix Processes Control
  • TTP3121 Chapter 3: File Manipulation
  • TTP3121 Chapter 2: Introduction to Unix Based OS
  • Signal

    It could be sent using kill() Unix System Call# by specifying the target process ID and the signal sent, or raise() to send Signal to the current process. SIGALRM can be raised using the system call alarm() with a parameter that specify when will the signal be delivered in seconds. pause() can be used to put the calling process into suspend until there is a Signal raised.

  • Semaphore in System V

    We can access (with flag values of 0) or create (with flag IPC_CREAT) a semaphore with semget() System Call# with parameters of IPC Facilities Key# the number of semaphores to be created, and the flags (including semaphore’s permission). semop() is used to release (increment) and obtain (decrement) semaphore or test for zero. It includes using a sembuf struct to specify the operation to be performed on each semaphore in the semaphore set identified by its ID. The detail are shown below:

  • Program

    Program is an executable file in Unix-based system such as Linux# and #macos, which could be a shell script, shell command, compiled file or object code file. It is created by a link editor and stored on a disk, and can be executed by the exec System Call# which will create a Process#.

  • Process

    Every process has its own unique process ID (PID), which could be obtained via getid() System Call#. We could get its parent’s PID too using getppid(). The process will inherit the user ID and group ID, which could be inspected with getuid() and getgid() or geteuid() and getegid() (getting the effective user ID and group ID, usually is the same with the user ID and group ID).

    Process is an instance of a #Program that is being executed. fork System Call# could be used to create a new Process. The process will inherit all the environment variables from the parent process. In #c, this could be accessed via function getenv(), which will either return a pointer to the specified environment variable or a NULL pointer, or third argument to the main function (shown below). The latter will be terminated with a NULL pointer.

  • Pipe

    Pipe provides a one-way flow of data, where the output of a process is directed into the input of another, which can be created using pipe() #Unix System Call. Pass in a file descriptor array (of type int) with a size of 2, the system call will return 2 file descriptors for use: fd[0] for reading and fd[1] for writing. A typical use case of Pipe is to establish a communication channel# between two related processes. The maximum amount of data allowed in the pipe (OS treats it like a Queue#) is usually 5120 bytes. In #unix, Pipe is implemented using #Unix Domain Sockets.

  • POSIX Mandatory Lock

    POSIX Mandatory Lock is a lock checked by Unix-like operating system to verify that the operation such as read and write does not interfere with a lock held by a process. It could be done by lockf() #Unix System Call (imported from unistd.h) which requires File Descriptor#, function used, and the length of the file. The locking starts from the current byte position of the file, and if the length is positive, then it will extend forward, otherwise it will extend backward. If length is 0, then the entire file is locked.

  • POSIX Advisory Lock

    POSIX Advisory Lock is a lock that doesn’t necessary forbid other process from writting the file that is being locked. It is especially useful when working with cooperating processes. It could be done with flock() Unix System Call#, imported from sys/file.h, with File Descriptor# and specified operation as parameters.

  • Named Pipe (FIFO)

    Creating FIFO can be done by calling the #Unix System Call mkfifo() (exported with the sys/types.h and sys/stat.h library) with the name of the FIFO file and permission of the FIFO file passed in as parameters. open() must be used on the FIFO file in order to have access to the pipe. An example of FIFO creation is shown below:

  • IPC Facilities Key

    If the flag passed to the IPC object System Call (#Message Queue, semaphore, and share memory) are IPC_CREAT and IPC_EXCL (exclusive), there is an additional need to check against the error number EEXIST which indicates that the IPC object has already existed.

  • I/O Multiplexing

    There will be the case where asynchronous I/O is desired in order to serve different I/O events at the same time. For example, a Program# might have to serve two read requests from different Processes. However, since serving one read request will always block another, it is unwise to listen just one of the socket and wait for it since the program can’t know which one will arrive first. It is the same concern for asynchronous Socket Programming where the server might need to serve two sockets at the same time while being ignorant about the order of their arrival. I/O Multiplexing using three Unix System Call# : select(), pselect(), and poll().

  • Get Hostname in C

    We can get the hostname of the local machine with the function gethostname() by passing an empty string and its length. The string will be filled by the hostname returned by the function upon successful #Unix System Call.

#operating-system #unix #macos #)