Linux System Calls: Process Control, File Manipulation, and I/O Operations, Study notes of Computer Science

An overview of various linux system calls related to process control, file manipulation, and i/o operations. Topics include fork, execl, wait, signal, open, read, write, creat, unlink, chmod, fcntl, mmap, and munmap. Understand how these system calls are used to create, manage, and manipulate files and processes.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-o1p
koofers-user-o1p 🇺🇸

5

(1)

10 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 201
The Unix System
Interface
Gerson Robboy
Portland State University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Linux System Calls: Process Control, File Manipulation, and I/O Operations and more Study notes Computer Science in PDF only on Docsity!

CS 201

The Unix System

Interface

Gerson Robboy

Portland State University

15-213, F’ 02

System Calls

A system call is a request to the operating system for a

A system call is a request to the operating system for a

service.

service.

 typically made via a trap into the kernel

The UNIX system call interfaces are defined in section 2

The UNIX system call interfaces are defined in section 2

of the man pages

of the man pages

 “man 2 intro” shows how they are actually called

These are the

These are the

real

real

UNIX services, everything else is

UNIX services, everything else is

 An abstraction

 Built on top of them

 See unistd.h in Linux kernel tree for details

15-213, F’ 02

File Manipulation

open

open

get a file descriptor for named file

get a file descriptor for named file

close

close

free a file descriptor

free a file descriptor

read

read

read

read data from a file descriptor

data from a file descriptor

write

write

write

write data to a file descriptor

data to a file descriptor

stat

stat

get file meta data

get file meta data

dup

dup

duplicate a descriptor

duplicate a descriptor

lseek lseek – – change the current offsetchange the current offset

creat

creat

create/rewrite a named file

create/rewrite a named file

unlink

unlink

remove a directory entry

remove a directory entry

chmod

chmod : change permissions associated with file

: change permissions associated with file

fcntl

fcntl : file control

: file control

mmap

mmap : map file contents

: map file contents

We’ve seen

these in action

already

We’ll look at

these

15-213, F’ 02

creat System Call

creat

creat

is used to create new files

is used to create new files

 int creat(const char *path, mode_t mode);

 Equivalent to

 open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)

If file already exists, truncates to zero

If file already exists, truncates to zero

#include <fcntl.h>

int fd;

mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

char *filename = "/tmp/file";

fd = creat(filename, mode);

15-213, F’ 02

Exercise

Write a simple “remove” utility that removes a single

file. The program must take one command argument,

the name of the file to remove. The program must

check to make sure there is exactly one argument,

then unlink the file and check to see whether it was

successfully unlinked.

15-213, F’ 02

chmod System Call

Change access permission mode of a file

Change access permission mode of a file

 int chmod(const char *path, mode_t mode);

 int fchmod(int fildes, mode_t mode);

The effective user ID of the process must match the

The effective user ID of the process must match the

owner of the file (or be 0)

owner of the file (or be 0)

Mode is constructed by the bitwise OR operation of

Mode is constructed by the bitwise OR operation of

the access permission bits

the access permission bits

 mode_t mode = S_ISUID | S_IRWXU | S_IRWXG;

15-213, F’ 02

fcntl System Call

#include <fcntl.h>

int fcntl(int fd, int cmd, /* arg / ...); / Number of args depends on

cmd */

A whole collection of things you can do with files

A whole collection of things you can do with files

 Make I/O synchronous or asynchronous

 File locking

 Receive signal when file is available for read/write

 Receive signal when file is modified by another process

 Keep file open across exec

 Anything else a particular device driver needs to do

F_DUPFD duplicates a file descriptor

F_DUPFD duplicates a file descriptor

 Find the lowest numbered available file descriptor greater than or

equal to arg and make it be a copy of fd

 Compare to dup2 system call

 dup2 returns the named descriptor and will close the named descriptor

if it was in use before the call

15-213, F’ 02

fcntl example

You want to write tests for a new processor

You want to write tests for a new processor

 You want to do this using a Linux kernel

 Ability to run many processes, do I/O, handle interrupts, etc

 Really exercise the system, not just individual CPU tests in isolation

 There are things you need to do in supervisor mode

 You don t want to rewrite the Linux kernel or define a new system

call interface

Write a device driver for a pseudo-device

Write a device driver for a pseudo-device

 dynamically linked to the kernel at run time

 Open this “device” as a file, and you can do fcntl on it

 Individual supervisor mode operations can be fcntl commands

Write your tests as user-space programs

Write your tests as user-space programs

 Can be multi-process, multi-threaded, do I/O, do whatever you want

with memory

 System calls to do small, specific operations

15-213, F’ 02

mmap() maps a file into your memory space.

mmap() maps a file into your memory space.

Instead of a read/write paradigm, you use a memory

Instead of a read/write paradigm, you use a memory

access paradigm.

access paradigm.

What does this buy you?

What does this buy you?

Does it improve performance by avoiding the overhead

Does it improve performance by avoiding the overhead

of system calls?

of system calls?

15-213, F’ 02

Directory Manipulation

Directories, while technically files, are handled in a

Directories, while technically files, are handled in a

special manner

special manner

opendir

opendir

readdir

readdir

closedir

closedir

rewinddir

rewinddir

15-213, F’ 02

readdir

Read a directory entry

Read a directory entry

 int readdir(unsigned int fd, struct dirent *dirp, unsigned int

count)

Reads the next directory entry to

Reads the next directory entry to

dirp

dirp

. Count is ignored.

. Count is ignored.

struct dirent

long d_ino; / inode number /

off_t d_off; / offset to this dirent /

unsigned short d_reclen; / length of this d_name /

char d_name [NAME_MAX+1]; / file name (null-terminated) /

15-213, F’ 02

closedir and rewinddir

closedir

closedir

closes the directory stream

closes the directory stream

 Equivalent to the close system call for files

rewinddir

rewinddir

resets the position of the directory stream to

resets the position of the directory stream to

the beginning

the beginning

 Seeks to the first entry in the directory

seekdir

seekdir

sets the location of the directory stream

sets the location of the directory stream

 Equivalent to the seek system call for files

What kind of program would use these?

What kind of program would use these?

15-213, F’ 02

Memory Allocation

Malloc, calloc, realloc, free, alloca

Malloc, calloc, realloc, free, alloca

These are really library calls, not system calls.

These are really library calls, not system calls.

The actual system call is

The actual system call is

sbrk

sbrk

 Sets the limit on the heap you are allowed to use

 Essentially, gives us a hunk of virtual address space to use

 But as users, we don ’ t normally call sbrk

Malloc, free and friends manage the memory

Malloc, free and friends manage the memory

 No system calls

 Just touch the virtual address, and the VM system allocates

the actual memory we need.

15-213, F’ 02

Memory Allocation

The main players

The main players

 malloc

 calloc

 realloc

 free

 alloca

Kernel Virtual Memory

User Stack

(created at run time)

Unused

Kernel Virtual Memory

User Stack

(created at run time)

Shared Libraries

(memory mapped)

Run-time Heap

(created at run time)

Read-only code and data

Read/write data

0xffffffff

0xc

0x

0x