Selected Linux system calls
CS 273 (OS), Fall 2020
Basic file handling
open()
read()
write()
close()
creat()
- Often, just use
O_CREAT
flag withopen()
instead lseek()
mknod()
- Example
-
~cs273/egs/mopen.c
File properties
fstat()
- Writes values into
struct stat
variable allocated by caller chmod()
chown()
umask()
- arg: bitmask of bits to turn off when caller creates new file or dir
Directory operations
mkdir()
rmdir()
link()
symlink()
unlink()
chdir()
Other file-related calls
dup()
dup2()
pipe()
- Provides two file descriptors for reading resp. writing
- Examples
-
ls -s | sort -n
ls -s | sort -n | grep -v '^total' > ls.out
utime()
- set access and/or modification time for a file
File systems
mount()
umount()
chroot()
Input/output
ioctl()
- Example
-
~cs273/egs/tryraw.sh
Memory management
brk()
sbrk()
- Note
-
These calls manage the total memory allocation for a process
Use library routinemalloc
for dynamic memory allocation
Time
time()
- seconds since the Epoch
times()
- utime, stime for calling process
gettimeofday()
- {sec, μsec} since the Epoch
settimeofday()
stime()
- set number of seconds since the Epoch
Processes
fork()
execve()
exit()
wait()
waitpid()
- Examples
-
~cs273/egs/forkeg.c
ls
emacs &
ls > ls.out
ls -s | sort -n
Process identification
getpid()
getgid()
- Current
gid
for caller getuid()
getppid()
- Return pid of parent
getpgrp()
- Return process group id of caller
setgid()
setuid()
getegid()
geteuid()
- Effective uid, different when SETUID bit set or
su
invoked setegid()
seteuid()
setpgrp()
- Assign a process to another pgid in the same session
Signals
signal()
kill()
alarm()
pause()
setitimer()
- Example
-
~cs273/egs/signal.sh
Miscellaneous process related calls
BSD-style networking
socket()
- Create an endpoint for communication
bind()
- Associate a network-scope port number with a socket
listen()
- Create a queue for pending connection requests
accept()
- Receive a connection request, return a fresh socket for future comm
connect()
- Request a connection to a particular host, port
send()
recv()
shutdown()
- Close one or both communication directions for a socket
close()
- Disconnect socket descriptor for the calling process
- Example
-
Client Server int sock; /* descriptor to access server */
| int ssock; /* descr. to contact this server */
| int csock; /* descr. to access client */
| sock = socket(...);
| ssock = socket(...);
| bind(ssock, ...);
| listen(ssock, ...);
| csock = accept(ssock, ...);
---
-
---
connect(sock, ...);
| count = send(socket, msg, msg_len);
count = recv(socket, buff, buff_len);
... ---
-
---
close(sock);
| shutdown(csock, how);
| close(csock);
More networking
getsockopt()
- Example:
SO_LINGER
, delay completion of exit() if sent data has not yet been received setsockopt()
gethostname()
sethostname()
select()