๐Ÿ“— -> 01/09/26: System Calls


Lecture link

๐ŸŽค Vocab

โ— Unit and Larger Context

โœ’๏ธ -> Scratch Notes

C program example

exec_detector.c

Carefully opens a file, and checks if first bytes indicate that the files are executable

  • 0x74 | 0x45 | 0x4c | 0x46 - Means file is ELF form (Executable and Linkable Format, common linux)
  • # | ! - Means its a script
  • Else, not executable

Lesson:
This file uses headers - #include <fcntl.h>
Headers are linked at compile time and dynamically loaded at runtime

  • Sometimes, needs to request privileged operations from OS

Clib Perms

memset - Regular func, only needs to have access to array buf
open - Always privileged function

  • Verify file exists/is permitted for user/sucessfully reads
  • Sensitive operations handled by OS through syscalls
    printf - Sometimes privileged function
  • printf points to stdout which is generally internally buffered
  • Flushing requires OS to write characters (syscall function write)
printf_write.c
printf("Hello ");
sleep(2);
printf("world!\n");

write(STDOUT_FILENO, "Hello ", 6);
sleep(2);
write(STDOUT_FILENO, "world!\n ", 7);

/*
$ /printf_write
<wait 2 sec>
Hello world!
Hello <wait 2 sec>world!
*\

First one is buffered, not printed immediately.
Second one prints immediately.

Sys calls

Def:
  • Specific CPU instruction
  • Immediate transfer of control to kernel code
    Serves as a secure API between user apps and OS kernel
    Handles:
  • Process management
  • Files and dirs
  • Memory
  • โ€ฆ

Process Management

Process - a program in execution
Has:

  • A process ID (PID)
  • Its own memory space
  • Representation in the OS through a Process Control Block (PCB)
    • A data structure
    • PID, state, CPU register copies for context switching, open files, etc

Main syscalls

Process creation and execution
	`fork()`: Create a new (clone) process
	`exec()`: Change executed program within running process
Process termination
	`exit()`: End running process
	`wait()/waitpid()`: Wait for a child process and collect exit code
Process identification
	`getpid()`: Get process PID
	`getppid()`: Get parent process PID
fork()
  • Duplicates process into a clone process
    • Child gets an almost identical copy of parent
      • Open files, CLI args, mem, stack, etc
    • Child resumes at fork
  • Fork returns a value, used to distinguish parent and child
    • PID of the child to the parent ( if PID > 0 printf("Parent") )
    • zero to the child ( if PID == 0 printf("Child") )
    • -1 if error ( if PID < 0 printf("Oops") )
  • Output might not be guaranteed, OS scheduling is involved
exec()
  • Process starts executing another program
exit()
  • Terminate current process
  • Can return an exit val
wait() / waitpid()

Waits for child processes to finish

system()

??

Difference between wait and waitpid

  • Fork only has one child, so what is difference between specific and any child? nested forks? different commands?
  • when multiple children

๐Ÿงช -> Refresh the Info

Did you generally find the overall content understandable or compelling or relevant or not, and why, or which aspects of the content were most novel or challenging for you and which aspects were most familiar or straightforward?)

Did a specific aspect of the content raise questions for you or relate to other ideas and findings youโ€™ve encountered, or are there other related issues you wish had been covered?)

Resources

  • Put useful links here

Connections

  • Link all related words