Reformat code

This commit is contained in:
Fabio Scotto di Santolo
2025-07-04 10:39:07 +02:00
parent 2bea38d7fc
commit dd53e83af6
28 changed files with 632 additions and 524 deletions

16
.clang-format Normal file
View File

@@ -0,0 +1,16 @@
BasedOnStyle: LLVM
IndentWidth: 8
TabWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
ColumnLimit: 80
AlignConsecutiveDeclarations: false
AlignConsecutiveAssignments: false
AlignEscapedNewlines: Left
AlignOperands: false
IndentCaseLabels: false
SpaceBeforeParens: ControlStatements

View File

@@ -9,6 +9,7 @@ Welcome! This is a collection of chapter summaries from the book **Linux System
- [Chapter 3 Buffered I/O](chp3/README.md) - [Chapter 3 Buffered I/O](chp3/README.md)
- [Chapter 4 Advanced File I/O](chp4/README.md) - [Chapter 4 Advanced File I/O](chp4/README.md)
- [Chapter 5 - Process Management](chp5/README.md) - [Chapter 5 - Process Management](chp5/README.md)
- [Chapter 6 - Advanced Process Management](chp6/README.md)
- [Exercises](exercises/README.md) - [Exercises](exercises/README.md)
> Each file contains an English summary of the chapter's key concepts. > Each file contains an English summary of the chapter's key concepts.

View File

@@ -2,8 +2,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void)
int fd = open("test.txt", O_CREAT | O_RDWR); {
// close(fd); int fd = open("test.txt", O_CREAT | O_RDWR);
return EXIT_SUCCESS; // close(fd);
return EXIT_SUCCESS;
} }

View File

@@ -1,7 +1,7 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void) int main(void)
{ {

View File

@@ -1,12 +1,13 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void) int main(void)
{ {
int fd; int fd;
fd = open("test.txt", O_CREAT | O_EXCL, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); fd = open("test.txt", O_CREAT | O_EXCL,
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (fd == -1) if (fd == -1)
perror("open"); perror("open");
close(fd); close(fd);

View File

@@ -6,51 +6,56 @@
#define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 500
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void)
printf("Try to open file read.txt\n"); {
int fd = open("read.txt", O_RDONLY); printf("Try to open file read.txt\n");
if (fd == -1) { int fd = open("read.txt", O_RDONLY);
perror("Failed to open read.txt"); if (fd == -1) {
return -1; perror("Failed to open read.txt");
} return -1;
printf("Open file success\n"); }
printf("Open file success\n");
int len; int len;
// move file's cursor at the end of file and return bytes // move file's cursor at the end of file and return bytes
if ((len = lseek(fd, 0, SEEK_END)) == -1) { if ((len = lseek(fd, 0, SEEK_END)) == -1) {
perror("Failed to move cursor at end of read.txt file"); perror("Failed to move cursor at end of read.txt file");
return -1; return -1;
} }
// cannot rewind cursor at the start file because pread use "bytes_read_total" for position // cannot rewind cursor at the start file because pread use
// "bytes_read_total" for position
printf("File size of %d bytes\n", len); printf("File size of %d bytes\n", len);
printf("Reading file read.txt\n"); printf("Reading file read.txt\n");
char buf[len + 1]; char buf[len + 1];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
size_t bytes_read_total = 0; size_t bytes_read_total = 0;
ssize_t ret; ssize_t ret;
while (bytes_read_total < len && (ret = pread(fd, buf, len - bytes_read_total, bytes_read_total)) != -1) { while (bytes_read_total < len &&
if (ret == -1) { (ret = pread(fd, buf, len - bytes_read_total,
if (errno == EINTR) continue; bytes_read_total)) != -1) {
perror("Failed to read read.txt"); if (ret == -1) {
break; if (errno == EINTR)
} continue;
bytes_read_total += ret; perror("Failed to read read.txt");
} break;
}
bytes_read_total += ret;
}
buf[bytes_read_total] = '\0'; buf[bytes_read_total] = '\0';
printf("File text is:\n\"%s\"\n", buf); printf("File text is:\n\"%s\"\n", buf);
printf("Close file read.txt\n"); printf("Close file read.txt\n");
if (close(fd) == -1) { if (close(fd) == -1) {
perror("Failed to close read.txt"); perror("Failed to close read.txt");
return -1; return -1;
} }
return 0; return 0;
} }

View File

@@ -5,51 +5,55 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void)
printf("Try to open file read.txt\n"); {
int fd = open("read.txt", O_RDONLY); printf("Try to open file read.txt\n");
if (fd == -1) { int fd = open("read.txt", O_RDONLY);
perror("Failed to open read.txt"); if (fd == -1) {
return -1; perror("Failed to open read.txt");
} return -1;
printf("Open file success\n"); }
printf("Open file success\n");
int len; int len;
if ((len = lseek(fd, 0, SEEK_END)) == -1) { if ((len = lseek(fd, 0, SEEK_END)) == -1) {
perror("Failed to move cursor at end of read.txt file"); perror("Failed to move cursor at end of read.txt file");
return -1; return -1;
} }
printf("File size of %d bytes\n", len); printf("File size of %d bytes\n", len);
if (lseek(fd, 0, SEEK_SET) == -1) { if (lseek(fd, 0, SEEK_SET) == -1) {
perror("Failed to move cursor at start of read.txt file"); perror("Failed to move cursor at start of read.txt file");
return -1; return -1;
} }
printf("Reading file read.txt\n"); printf("Reading file read.txt\n");
char buf[len + 1]; char buf[len + 1];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
size_t bytes_read_total = 0; size_t bytes_read_total = 0;
ssize_t ret; ssize_t ret;
while (bytes_read_total < len && (ret = read(fd, buf + bytes_read_total, len - bytes_read_total)) != 0) { while (bytes_read_total < len &&
if (ret == -1) { (ret = read(fd, buf + bytes_read_total,
if (errno == EINTR) continue; len - bytes_read_total)) != 0) {
perror("Failed to read read.txt"); if (ret == -1) {
break; if (errno == EINTR)
} continue;
bytes_read_total += ret; perror("Failed to read read.txt");
} break;
}
bytes_read_total += ret;
}
buf[bytes_read_total] = '\0'; buf[bytes_read_total] = '\0';
printf("File text is:\n\"%s\"\n", buf); printf("File text is:\n\"%s\"\n", buf);
printf("Close file read.txt\n"); printf("Close file read.txt\n");
if (close(fd) == -1) { if (close(fd) == -1) {
perror("Failed to close read.txt"); perror("Failed to close read.txt");
return -1; return -1;
} }
return 0; return 0;
} }

View File

@@ -1,7 +1,8 @@
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
int main(void) { int main(void)
{
int ret; int ret;
ret = truncate("./pirate.txt", 45); ret = truncate("./pirate.txt", 45);

View File

@@ -1,10 +1,11 @@
#include <poll.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <poll.h>
#define TIMEOUT 5 /* poll timeout, in seconds */ #define TIMEOUT 5 /* poll timeout, in seconds */
int main(void) { int main(void)
{
struct pollfd fds[2]; struct pollfd fds[2];
int ret; int ret;

View File

@@ -6,7 +6,8 @@
#define TIMEOUT 5 #define TIMEOUT 5
#define BUF_LEN 1024 #define BUF_LEN 1024
int main(void) { int main(void)
{
struct timeval tv; struct timeval tv;
fd_set readfds; fd_set readfds;
int ret; int ret;
@@ -20,13 +21,7 @@ int main(void) {
tv.tv_usec = 0; tv.tv_usec = 0;
/* All right, now block! */ /* All right, now block! */
ret = select( ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
STDIN_FILENO + 1,
&readfds,
NULL,
NULL,
&tv
);
if (ret == -1) { if (ret == -1) {
perror("select"); perror("select");
return 1; return 1;
@@ -42,7 +37,7 @@ int main(void) {
* nonzero, but we will humor ourselves.) * nonzero, but we will humor ourselves.)
*/ */
if (FD_ISSET(STDIN_FILENO, &readfds)) { if (FD_ISSET(STDIN_FILENO, &readfds)) {
char buf[BUF_LEN+1]; char buf[BUF_LEN + 1];
int len; int len;
/* guaranteed to not block */ /* guaranteed to not block */

View File

@@ -1,11 +1,11 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h> #include <string.h>
#include <sys/time.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#define FILE_NUMBER 3 #define FILE_NUMBER 3
#define FILENAME_MAX_LEN 10 #define FILENAME_MAX_LEN 10
@@ -15,7 +15,8 @@
int create_tmp_file(char *filename); int create_tmp_file(char *filename);
void cleanup(char *filenames[]); void cleanup(char *filenames[]);
int main(void) { int main(void)
{
int exit_code = 0; int exit_code = 0;
char *filename_template = "test%d.txt"; char *filename_template = "test%d.txt";
char *filenames[FILE_NUMBER] = {NULL}; char *filenames[FILE_NUMBER] = {NULL};
@@ -23,22 +24,25 @@ int main(void) {
// Create a bunch of number ephemeral files // Create a bunch of number ephemeral files
for (int i = 0; i < FILE_NUMBER; i++) { for (int i = 0; i < FILE_NUMBER; i++) {
char *filename = (char *)malloc(FILENAME_MAX_LEN*sizeof(char)); char *filename =
(char *)malloc(FILENAME_MAX_LEN * sizeof(char));
if (!filename) { if (!filename) {
perror("malloc failed"); perror("malloc failed");
exit_code = -1; exit_code = -1;
goto exit; goto exit;
} }
snprintf(filename, FILENAME_MAX_LEN, filename_template, i+1); snprintf(filename, FILENAME_MAX_LEN, filename_template, i + 1);
filenames[i] = filename; filenames[i] = filename;
printf("Opening file %s...\n", filename); printf("Opening file %s...\n", filename);
fds[i] = create_tmp_file(filename); fds[i] = create_tmp_file(filename);
if (fds[i] == -1) { if (fds[i] == -1) {
fprintf(stderr, "Error to open file %s: %s\n", filename, strerror(errno)); fprintf(stderr, "Error to open file %s: %s\n", filename,
strerror(errno));
exit_code = -1; exit_code = -1;
goto exit; goto exit;
} }
printf("Open file %s with file descriptor %d\n", filename, fds[i]); printf("Open file %s with file descriptor %d\n", filename,
fds[i]);
// write something in the files // write something in the files
dprintf(fds[i], "Hello I am %s\n", filename); dprintf(fds[i], "Hello I am %s\n", filename);
@@ -50,21 +54,17 @@ int main(void) {
FD_SET(fds[i], &readfds); FD_SET(fds[i], &readfds);
} }
struct timeval *timeout = (struct timeval *)malloc(sizeof(struct timeval)); struct timeval *timeout =
(struct timeval *)malloc(sizeof(struct timeval));
timeout->tv_sec = TIMEOUT; timeout->tv_sec = TIMEOUT;
timeout->tv_usec = 0; timeout->tv_usec = 0;
/* This function do not work with regular files because for the operating system, /* This function do not work with regular files because for the
* a regular file is ready always. * operating system, a regular file is ready always. This example is
* This example is wrong the best way to use select() is with socket, pipe or other channel types. * wrong the best way to use select() is with socket, pipe or other
* channel types.
*/ */
int ret = select( int ret = select(FILE_NUMBER + 1, &readfds, NULL, NULL, timeout);
FILE_NUMBER + 1,
&readfds,
NULL,
NULL,
timeout
);
if (ret == -1) { if (ret == -1) {
fprintf(stderr, "Error to select: %s\n", strerror(errno)); fprintf(stderr, "Error to select: %s\n", strerror(errno));
@@ -79,10 +79,11 @@ int main(void) {
for (int i = 0; i < FILE_NUMBER; i++) { for (int i = 0; i < FILE_NUMBER; i++) {
int fd = fds[i]; int fd = fds[i];
if (FD_ISSET(fd, &readfds)) { if (FD_ISSET(fd, &readfds)) {
char buf[BUF_LEN+1]; char buf[BUF_LEN + 1];
int len = read(fd, buf, BUF_LEN); int len = read(fd, buf, BUF_LEN);
if (len == -1) { if (len == -1) {
fprintf(stderr, "Error reading file %s: %s\n", filenames[i], strerror(errno)); fprintf(stderr, "Error reading file %s: %s\n",
filenames[i], strerror(errno));
exit_code = 1; exit_code = 1;
goto exit; goto exit;
} }
@@ -99,7 +100,8 @@ exit:
int fd = fds[i]; int fd = fds[i];
printf("Close file with file descriptor %d\n", fd); printf("Close file with file descriptor %d\n", fd);
if (close(fd) == -1) { if (close(fd) == -1) {
fprintf(stderr, "Error to close file %d: %s\n", fd, strerror(errno)); fprintf(stderr, "Error to close file %d: %s\n", fd,
strerror(errno));
} }
} }
@@ -112,7 +114,8 @@ exit:
return exit_code; return exit_code;
} }
int create_tmp_file(char *filename) { int create_tmp_file(char *filename)
{
int fd = open(filename, O_CREAT | O_EXCL | O_RDWR, 0644); int fd = open(filename, O_CREAT | O_EXCL | O_RDWR, 0644);
if (fd == -1) { if (fd == -1) {
return -1; return -1;
@@ -120,11 +123,13 @@ int create_tmp_file(char *filename) {
return fd; return fd;
} }
void cleanup(char *filenames[]) { void cleanup(char *filenames[])
{
for (int j = 0; j < FILE_NUMBER; j++) { for (int j = 0; j < FILE_NUMBER; j++) {
if (filenames[j] != NULL) { if (filenames[j] != NULL) {
if (remove(filenames[j]) == -1) if (remove(filenames[j]) == -1)
fprintf(stderr, "Failed to remove %s: %s\n", filenames[j], strerror(errno)); fprintf(stderr, "Failed to remove %s: %s\n",
filenames[j], strerror(errno));
free(filenames[j]); free(filenames[j]);
filenames[j] = NULL; filenames[j] = NULL;
} }

View File

@@ -1,40 +1,42 @@
#include <stdio.h> #include <stdio.h>
int main(void) { int main(void)
FILE *in, *out; {
struct pirate { FILE *in, *out;
char name[100]; /* real name */ struct pirate {
unsigned long booty; /* in pounds sterling */ char name[100]; /* real name */
unsigned int beard_len; /* in inches */ unsigned long booty; /* in pounds sterling */
} p, blackbeard = {"Edward Teach", 950, 48}; unsigned int beard_len; /* in inches */
} p, blackbeard = {"Edward Teach", 950, 48};
out = fopen("data", "w"); out = fopen("data", "w");
if (!out) { if (!out) {
perror("fopen"); perror("fopen");
return 1; return 1;
} }
if (!fwrite(&blackbeard, sizeof(struct pirate), 1, out)) { if (!fwrite(&blackbeard, sizeof(struct pirate), 1, out)) {
perror("fwrite"); perror("fwrite");
return 1; return 1;
} }
if (fclose(out)) { if (fclose(out)) {
perror("fclose"); perror("fclose");
return 1; return 1;
} }
in = fopen("data", "r"); in = fopen("data", "r");
if (!in) { if (!in) {
perror("fopen"); perror("fopen");
return 1; return 1;
} }
if (!fread(&p, sizeof(struct pirate), 1, in)) { if (!fread(&p, sizeof(struct pirate), 1, in)) {
perror("fread"); perror("fread");
return 1; return 1;
} }
if (fclose(in)) { if (fclose(in)) {
perror("fclose"); perror("fclose");
return 1; return 1;
} }
printf("name=\"%s\" booty=%lu beard_len=%u\n", p.name, p.booty, p.beard_len); printf("name=\"%s\" booty=%lu beard_len=%u\n", p.name, p.booty,
return 0; p.beard_len);
return 0;
} }

View File

@@ -1,7 +1,8 @@
#include<stdio.h>
#include <limits.h> #include <limits.h>
#include <stdio.h>
int main(void) { int main(void)
printf("Limit buffer size is %u bytes.\n", LINE_MAX); {
printf("Buffer size is %u bytes.\n", BUFSIZ); printf("Limit buffer size is %u bytes.\n", LINE_MAX);
printf("Buffer size is %u bytes.\n", BUFSIZ);
} }

View File

@@ -1,10 +1,10 @@
#include <fcntl.h>
#include <linux/fs.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <linux/fs.h> #include <sys/stat.h>
#include <sys/types.h>
/* get_block - for the file associated with the given fd, returns /* get_block - for the file associated with the given fd, returns
* the physical block mapping to logical_block * the physical block mapping to logical_block
@@ -22,66 +22,71 @@ int get_nr_blocks(int fd);
*/ */
void print_blocks(int fd); void print_blocks(int fd);
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
if (argc < 2) { {
fprintf(stderr, "usage: %s <file>\n", argv[0]); if (argc < 2) {
return 1; fprintf(stderr, "usage: %s <file>\n", argv[0]);
} return 1;
}
int fd = open(argv[1], O_RDONLY); int fd = open(argv[1], O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror("open"); perror("open");
return 1; return 1;
} }
print_blocks(fd); print_blocks(fd);
return 0; return 0;
} }
int get_block(int fd, int logical_block) { int get_block(int fd, int logical_block)
int ret = ioctl(fd, FIBMAP, &logical_block); {
if (ret < 0) { int ret = ioctl(fd, FIBMAP, &logical_block);
perror("ioctl"); if (ret < 0) {
return -1; perror("ioctl");
} return -1;
return logical_block; }
return logical_block;
} }
int get_nr_blocks(int fd) { int get_nr_blocks(int fd)
struct stat buf; {
int ret = fstat(fd, &buf); struct stat buf;
if (ret < 0) { int ret = fstat(fd, &buf);
perror("fstat"); if (ret < 0) {
return -1; perror("fstat");
} return -1;
return buf.st_blocks; }
return buf.st_blocks;
} }
void print_blocks(int fd) { void print_blocks(int fd)
int nr_blocks = get_nr_blocks(fd); {
if (nr_blocks < 0) { int nr_blocks = get_nr_blocks(fd);
fprintf(stderr, "get_nr_blocks failed!\n"); if (nr_blocks < 0) {
return; fprintf(stderr, "get_nr_blocks failed!\n");
} return;
}
if (nr_blocks == 0) { if (nr_blocks == 0) {
printf("no allocated blocks\n"); printf("no allocated blocks\n");
return; return;
} else if (nr_blocks == 1) { } else if (nr_blocks == 1) {
printf("1 block\n"); printf("1 block\n");
} else { } else {
printf("%d blocks\n\n", nr_blocks); printf("%d blocks\n\n", nr_blocks);
} }
for (int i = 0; i < nr_blocks; i++) { for (int i = 0; i < nr_blocks; i++) {
int phys_block = get_block(fd, i); int phys_block = get_block(fd, i);
if (phys_block < 0) { if (phys_block < 0) {
fprintf(stderr, "get_block failed!\n"); fprintf(stderr, "get_block failed!\n");
return; return;
} }
if (!phys_block) continue; if (!phys_block)
continue;
printf("(%u, %u) ", i, phys_block); printf("(%u, %u) ", i, phys_block);
} }
putchar('\n'); putchar('\n');
} }

View File

@@ -1,39 +1,41 @@
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
/* get_inode - returns the inode of the file associated /* get_inode - returns the inode of the file associated
* with the given file descriptor, or -1 on failure. * with the given file descriptor, or -1 on failure.
*/ */
int get_inode(int fd); int get_inode(int fd);
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
if (argc < 2) { {
fprintf(stderr, "usage: %s <file>\n", argv[0]); if (argc < 2) {
return 1; fprintf(stderr, "usage: %s <file>\n", argv[0]);
} return 1;
}
int fd = open(argv[1], O_RDONLY); int fd = open(argv[1], O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror("open"); perror("open");
return 1; return 1;
} }
int inode = get_inode(fd); int inode = get_inode(fd);
printf("%d\n", inode); printf("%d\n", inode);
return 0; return 0;
} }
int get_inode(int fd) { int get_inode(int fd)
struct stat buf; {
struct stat buf;
int ret = fstat(fd, &buf); int ret = fstat(fd, &buf);
if (ret < 0) { if (ret < 0) {
perror("fstat"); perror("fstat");
return -1; return -1;
} }
return buf.st_ino; return buf.st_ino;
} }

View File

@@ -1,11 +1,12 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <stdio.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "usage: %s <file>\n", argv[0]); fprintf(stderr, "usage: %s <file>\n", argv[0]);

View File

@@ -1,41 +1,42 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void)
char foo[48], bar[51], baz[49]; {
int fd = open("buccaneer.txt", O_RDONLY); char foo[48], bar[51], baz[49];
if (fd == -1) { int fd = open("buccaneer.txt", O_RDONLY);
perror("open"); if (fd == -1) {
return 1; perror("open");
} return 1;
}
// setup our iovec structures // setup our iovec structures
struct iovec iov[3]; struct iovec iov[3];
iov[0].iov_base = foo; iov[0].iov_base = foo;
iov[0].iov_len = sizeof(foo); iov[0].iov_len = sizeof(foo);
iov[1].iov_base = bar; iov[1].iov_base = bar;
iov[1].iov_len = sizeof(bar); iov[1].iov_len = sizeof(bar);
iov[2].iov_base = baz; iov[2].iov_base = baz;
iov[2].iov_len = sizeof(baz); iov[2].iov_len = sizeof(baz);
// read into the structures with a single call // read into the structures with a single call
int nr = readv(fd, iov, 3); int nr = readv(fd, iov, 3);
if (nr == -1) { if (nr == -1) {
perror("readv"); perror("readv");
return 1; return 1;
} }
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
printf("%d: %s", i, (char *)iov[i].iov_base); printf("%d: %s", i, (char *)iov[i].iov_base);
if (close(fd)) { if (close(fd)) {
perror("close"); perror("close");
return 1; return 1;
} }
return 0; return 0;
} }

View File

@@ -1,45 +1,44 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void)
struct iovec iov[3]; {
struct iovec iov[3];
char *buf[] = { char *buf[] = {"The term buccaneer comes from the word boucan.\n",
"The term buccaneer comes from the word boucan.\n", "A boucan is a wooden frame used for cooking meet.\n",
"A boucan is a wooden frame used for cooking meet.\n", "Buccaneer is the West Indies name for a pirate.\n"};
"Buccaneer is the West Indies name for a pirate.\n"
};
int fd = open("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC); int fd = open("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC);
if (fd == -1) { if (fd == -1) {
perror("open"); perror("open");
return 1; return 1;
} }
// fill out three iovec structures // fill out three iovec structures
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
iov[i].iov_base = buf[i]; iov[i].iov_base = buf[i];
iov[i].iov_len = strlen(buf[i]) + 1; iov[i].iov_len = strlen(buf[i]) + 1;
} }
// with a single call, write them all out // with a single call, write them all out
ssize_t nr = writev(fd, iov, 3); ssize_t nr = writev(fd, iov, 3);
if (nr == -1) { if (nr == -1) {
perror("writev"); perror("writev");
return 1; return 1;
} }
printf("wrote %lu bytes\n", nr); printf("wrote %lu bytes\n", nr);
if (close(fd)) { if (close(fd)) {
perror("close"); perror("close");
return 1; return 1;
} }
return 0; return 0;
} }

View File

@@ -1,15 +1,16 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void out(void) { void out(void)
printf("atexit() succeeded!\n"); {
printf("atexit() succeeded!\n");
} }
int main(void)
int main(void) { {
if (atexit(out)) { if (atexit(out)) {
fprintf(stderr, "atexit() failed\n"); fprintf(stderr, "atexit() failed\n");
return 1; return 1;
} }
return 0; return 0;
} }

View File

@@ -1,43 +1,46 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#define NR_OPEN 1048576 #define NR_OPEN 1048576
int main(void) { int main(void)
// create new process {
pid_t pid = fork(); // create new process
if (pid == -1) { pid_t pid = fork();
return -1; if (pid == -1) {
} else if (pid != 0) { return -1;
printf("Running daemon with pid %d\n", pid); } else if (pid != 0) {
exit(EXIT_SUCCESS); printf("Running daemon with pid %d\n", pid);
} exit(EXIT_SUCCESS);
}
// create a new session and process group // create a new session and process group
if (setsid() == -1) return -1; if (setsid() == -1)
return -1;
// set the working directory to the root directory // set the working directory to the root directory
if (chdir("/") == -1) return -1; if (chdir("/") == -1)
return -1;
// close all open files--NR_OPEN is overkill, but works // close all open files--NR_OPEN is overkill, but works
for (int i = 0; i < NR_OPEN; i++) { for (int i = 0; i < NR_OPEN; i++) {
close(i); close(i);
} }
// redirect fd's 0,1,2 to /dev/null // redirect fd's 0,1,2 to /dev/null
open("/dev/null", O_RDWR); // stdin open("/dev/null", O_RDWR); // stdin
dup(0); // stdout dup(0); // stdout
dup(0); // stderr dup(0); // stderr
// do its daemon thing // do its daemon thing
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
printf("Hello from daemon %d\n", getpid()); printf("Hello from daemon %d\n", getpid());
sleep(1000); sleep(1000);
} }
return 0; return 0;
} }

View File

@@ -2,8 +2,9 @@
#define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 500
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void)
printf("My session id=%d\n", getsid(0)); {
printf("My process group id=%d\n", getpgid(0)); printf("My session id=%d\n", getsid(0));
return 0; printf("My process group id=%d\n", getpgid(0));
return 0;
} }

View File

@@ -1,9 +1,9 @@
#include <stdio.h> #include <stdio.h>
#define _XOPEN_SOURCE #define _XOPEN_SOURCE
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h>
/* my_system - synchronously spawns and waits for the command /* my_system - synchronously spawns and waits for the command
* "/bin/sh -c <cmd>". * "/bin/sh -c <cmd>".
@@ -13,51 +13,53 @@
*/ */
int my_system(const char *); int my_system(const char *);
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
if (argc < 2) { {
fprintf(stderr, "usage: %s <command>", argv[0]); if (argc < 2) {
return EXIT_FAILURE; fprintf(stderr, "usage: %s <command>", argv[0]);
} return EXIT_FAILURE;
}
const char *command = argv[1]; const char *command = argv[1];
pid_t pid = fork(); pid_t pid = fork();
if (pid == -1) { if (pid == -1) {
perror("fork"); perror("fork");
return EXIT_FAILURE; return EXIT_FAILURE;
} else if (pid > 0) { } else if (pid > 0) {
int ret = my_system(command); int ret = my_system(command);
if (ret == -1) { if (ret == -1) {
fprintf(stderr, "command: \"%s\" error", command); fprintf(stderr, "command: \"%s\" error", command);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return ret; return ret;
} }
int status; int status;
wait(&status); wait(&status);
return status; return status;
} }
int my_system(const char *cmd) { int my_system(const char *cmd)
pid_t pid = fork(); {
if (pid == -1) { pid_t pid = fork();
return -1; if (pid == -1) {
} else if (pid == 0) { return -1;
const char *argv[4]; } else if (pid == 0) {
argv[0] = "sh"; const char *argv[4];
argv[1] = "-c"; argv[0] = "sh";
argv[2] = cmd; argv[1] = "-c";
argv[3] = NULL; argv[2] = cmd;
argv[3] = NULL;
execv("/bin/sh", argv); execv("/bin/sh", argv);
exit(-1); exit(-1);
} }
int status; int status;
if (waitpid(pid, &status, 0) == -1) { if (waitpid(pid, &status, 0) == -1) {
return -1; return -1;
} else if (WIFEXITED(status)) { } else if (WIFEXITED(status)) {
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
return -1; return -1;
} }

View File

@@ -1,31 +1,34 @@
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h>
int main(void) { int main(void)
if (!fork()) {
return 1; if (!fork())
return 1;
int status; int status;
pid_t pid = wait(&status); pid_t pid = wait(&status);
if (pid == -1) { if (pid == -1) {
perror("wait"); perror("wait");
} }
printf("pid=%d\n", pid); printf("pid=%d\n", pid);
if (WIFEXITED(status)) if (WIFEXITED(status))
printf("Normal termination with exit status=%d\n", WEXITSTATUS(status)); printf("Normal termination with exit status=%d\n",
WEXITSTATUS(status));
if (WIFSIGNALED(status)) if (WIFSIGNALED(status))
printf("Killed by signal=%d%s\n", WTERMSIG(status), WCOREDUMP(status) ? " (dumped core)" : ""); printf("Killed by signal=%d%s\n", WTERMSIG(status),
WCOREDUMP(status) ? " (dumped core)" : "");
if (WIFSTOPPED(status)) if (WIFSTOPPED(status))
printf("Stopped by signal=%d\n", WSTOPSIG(status)); printf("Stopped by signal=%d\n", WSTOPSIG(status));
if (WIFCONTINUED(status)) if (WIFCONTINUED(status))
printf("Continued\n"); printf("Continued\n");
return 0; return 0;
} }

52
chp6/README.md Normal file
View File

@@ -0,0 +1,52 @@
# 📘 Chapter 6 Advanced Process Management (Summary)
**From _Linux System Programming_ by Robert Love, 2nd Ed.**
---
## 🔄 Process Scheduling
- The kernel scheduler selects **runnable processes** (those not blocked on I/O, etc.).
- Goals: **maximize CPU utilization**, minimize wait time, and ensure fairness :contentReference[oaicite:1]{index=1}.
- Covers **preemptive scheduling**, **timeslices**, and the Linux **Completely Fair Scheduler (CFS)**.
- Explains **processor affinity** and CPU sets: binding processes to specific CPUs for cache performance :contentReference[oaicite:2]{index=2}.
## 🧭 Yielding the Processor
- `sched_yield()` lets a running process give up the CPU voluntarily.
- Useful in busy-wait loops or when expecting immediate CPU from others :contentReference[oaicite:3]{index=3}.
## 🎚️ Priorities & Nice Values
- Processes can adjust their priority using **`nice()`**, where lower nice = higher priority.
- Real-time scheduling policies (`SCHED_FIFO`, `SCHED_RR`) grant stronger priority guarantees for time-sensitive tasks :contentReference[oaicite:4]{index=4}.
## 🧠 Processor Affinity
- **Get/set CPU affinity** with `sched_getaffinity()` and `sched_setaffinity()`.
- Ensures cache affinity, reducing context-switch overhead :contentReference[oaicite:5]{index=5}.
## ⏱️ Real-Time Scheduling
- Differences between **hard** and **soft real-time** systems.
- Configurable policies: `SCHED_FIFO`, `SCHED_RR`, with real-time priority range 099 :contentReference[oaicite:6]{index=6}.
- Care needed: real-time tasks can lead to starvation of normal tasks.
## 🔒 Resource Limits
- Kernel enforces per-process limits via `setrlimit()`/`getrlimit()`:
- CPU usage (`RLIMIT_CPU`)
- File sizes (`RLIMIT_FSIZE`)
- Number of open files, memory, etc. :contentReference[oaicite:7]{index=7}.
- Helps contain rogue or misbehaving processes, essential for robustness.
---
## ✅ Why It Matters
Advanced control over process scheduling, priorities, affinity, and resource limits is critical for writing:
- Real-time or latency-sensitive applications
- High-performance servers that benefit from core binding
- Robust daemons that avoid resource exhaustion
This chapter equips systems programmers with the tools to finely tune how tasks interact with the kernel scheduler and manage system impact.
---
## 📚 References
- *Linux System Programming*, 2nd Ed. — Robert Love
- `man 2 sched_setscheduler`, `man 2 setrlimit`, `man 2 sched_getaffinity`
- Wikipedia: [Completely Fair Scheduler](https://en.wikipedia.org/wiki/Completely_Fair_Scheduler) :contentReference[oaicite:8]{index=8}

View File

@@ -1,69 +1,71 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define BUFF_LEN 4096 #define BUFF_LEN 4096
int main(int argc, char *argv[])
{
// Check number of arguments
if (argc < 3) {
fprintf(stderr, "Usage: %s SOURCE DEST\n", argv[0]);
return EXIT_FAILURE;
}
int main(int argc, char *argv[]) { // Get arguments
// Check number of arguments const char *src = argv[1];
if (argc < 3) { const char *dst = argv[2];
fprintf(stderr, "Usage: %s SOURCE DEST\n", argv[0]);
return EXIT_FAILURE;
}
// Get arguments // Open source file in read only mode
const char *src = argv[1]; int srcfd = open(src, O_RDONLY);
const char *dst = argv[2]; if (srcfd == -1) {
perror("Failed open source file");
return EXIT_FAILURE;
}
// Open source file in read only mode // Open destination file in write mode, if exists trunc file otherwise
int srcfd = open(src, O_RDONLY); // create a new file
if (srcfd == -1) { int dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
perror("Failed open source file"); if (dstfd == -1) {
return EXIT_FAILURE; perror("Failed open destination file");
} if (close(srcfd) == -1) {
perror("Failed close source file");
}
return EXIT_FAILURE;
}
// Open destination file in write mode, if exists trunc file otherwise create a new file int exit_code = EXIT_SUCCESS;
int dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644); char buff[BUFF_LEN]; // reading data buffer
if (dstfd == -1) { ssize_t read_bytes;
perror("Failed open destination file"); while ((read_bytes = read(srcfd, buff, BUFF_LEN)) != 0) {
if (close(srcfd) == -1) { if (read_bytes == -1) {
perror("Failed close source file"); // There is an error for reading source file
} if (errno == EINTR)
return EXIT_FAILURE; continue;
} perror("Failed to read source file");
exit_code = EXIT_FAILURE;
break;
}
int exit_code = EXIT_SUCCESS; // Write buffer at destination file
char buff[BUFF_LEN]; // reading data buffer if (write(dstfd, buff, read_bytes) == -1) {
ssize_t read_bytes; perror("Error to write to destination file");
while ((read_bytes = read(srcfd, buff, BUFF_LEN)) != 0) { exit_code = EXIT_FAILURE;
if (read_bytes == -1) { break;
// There is an error for reading source file }
if (errno == EINTR) continue; }
perror("Failed to read source file");
exit_code = EXIT_FAILURE;
break;
}
// Write buffer at destination file // Try to close files
if (write(dstfd, buff, read_bytes) == -1) { if (close(srcfd) == -1) {
perror("Error to write to destination file"); perror("Failed to close source file");
exit_code = EXIT_FAILURE; exit_code = EXIT_FAILURE;
break; }
} if (close(dstfd) == -1) {
} perror("Failed to close destination file");
exit_code = EXIT_FAILURE;
}
// Try to close files return exit_code;
if (close(srcfd) == -1) {
perror("Failed to close source file");
exit_code = EXIT_FAILURE;
}
if (close(dstfd) == -1) {
perror("Failed to close destination file");
exit_code = EXIT_FAILURE;
}
return exit_code;
} }

View File

@@ -1,15 +1,15 @@
#include "tree.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "tree.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
const char *path = (argc > 1) ? argv[1] : "."; {
const char *path = (argc > 1) ? argv[1] : ".";
if (print_tree(path, 0) == -1) { if (print_tree(path, 0) == -1) {
perror("Error traversing directory"); perror("Error traversing directory");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@@ -1,35 +1,39 @@
#include "tree.h"
#include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "tree.h"
int print_tree(const char *path, int depth) { int print_tree(const char *path, int depth)
DIR *dir = opendir(path); {
if (!dir) return -1; DIR *dir = opendir(path);
if (!dir)
return -1;
struct dirent *entry; struct dirent *entry;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
// Skip . and .. // Skip . and ..
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) if (strcmp(entry->d_name, ".") == 0 ||
continue; strcmp(entry->d_name, "..") == 0)
continue;
for (int i = 0; i < depth; i++) printf(" "); for (int i = 0; i < depth; i++)
printf("|-- %s\n", entry->d_name); printf(" ");
printf("|-- %s\n", entry->d_name);
// Construct full path // Construct full path
char full_path[4096]; char full_path[4096];
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name); snprintf(full_path, sizeof(full_path), "%s/%s", path,
entry->d_name);
struct stat st; struct stat st;
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) { if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) {
print_tree(full_path, depth + 1); print_tree(full_path, depth + 1);
} }
} }
closedir(dir); closedir(dir);
return 0; return 0;
} }

View File

@@ -4,4 +4,3 @@
int print_tree(const char *path, int depth); int print_tree(const char *path, int depth);
#endif #endif