Computer Systems Fundamentals
Course Resources
Administrivia: | Course Outline | COMP1521 Handbook |
Administrivia: | Course Timetable | Help Sessions |
Meet the Team: | Our Team |
Platforms: | Lecture Recordings | Online Tut-Labs and Help Sessions (via BbCollaborate) | Course Forum |
Style Guides: | COMP1521 C Style Guide | Assembly Style Guide |
MIPS Resources: | MIPS Documentation | Text Editors for Assembly |
mipsy: | mipsy-web | mipsy source code | Debugging with mipsy (video) |
Resources: | Linux Cheatsheet | C Reference |
Assessment: | Autotests, Submissions, Marks | Give online: submission | Give online: sturec |
Assignments: | Assignment 1 | Assignment 2 |
Course Content Week-by-Week
- Tutorial
- Laboratory
- Extra Revision
- Monday Week 1
- Wednesday Week 1
- Tutorial
- Laboratory
- Weekly Test
- Monday Week 3
- Wednesday Week 3
- Tutorial
- Laboratory
- Weekly Test
- Monday Week 4
- Wednesday Week 4
- Tutorial
- Laboratory
- Weekly Test
- Monday Week 5
- Wednesday Week 5
- Weekly Test
- Extra Revision
Course Content Topic-by-Topic
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter a number: ");
scanf("%d", &a);
printf("Enter another number: ");
scanf("%d", &b);
printf("The sum of the squares of %d and %d is %d\n", a, b, a*a + b*b);
return 0;
}
Square two numbers and sum their squares.
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter a number: ");
scanf("%d", &a);
printf("Enter another number: ");
scanf("%d", &b);
printf("The sum of the squares of ");
printf("%d", a);
printf(" and ");
printf("%d", b);
printf(" is ");
a = a * a;
b = b * b;
printf("%d", a + b);
putchar('\n');
return 0;
}
Square and add two numbers and print the result.
.text
main:
# Locals:
# - $t0: int a
# - $t1: int b
li $v0, 4 # syscall 4: print_string
la $a0, prompt1_msg #
syscall # printf("Enter a number: ");
li $v0, 5 # syscall 5: read_int
syscall #
move $t0, $v0 # scanf("%d", &a);
li $v0, 4 # syscall 4: print_string
la $a0, prompt2_msg #
syscall # printf("Enter another number: ");
li $v0, 5 # syscall 5: read_int
syscall #
move $t1, $v0 # scanf("%d", &b);
li $v0, 4 # syscall 4: print_string
la $a0, result_msg_1 #
syscall # printf("The sum of the squares of ");
li $v0, 1 # syscall 1: print_int
move $a0, $t0 #
syscall # printf("%d", a);
li $v0, 4 # syscall 4: print_string
la $a0, result_msg_2 #
syscall # printf(" and ");
li $v0, 1 # syscall 1: print_int
move $a0, $t1 #
syscall # printf("%d", b);
li $v0, 4 # syscall 4: print_string
la $a0, result_msg_3 #
syscall # printf(" is ");
mul $t0, $t0, $t0 # a = a * a;
mul $t1, $t1, $t1 # b = b * b;
li $v0, 1 # syscall 1: print_int
add $a0, $t0, $t1 #
syscall # printf("%d", a + b);
li $v0, 11 # syscall 11: print_char
la $a0, '\n' #
syscall # putchar('\n');
li $v0, 0
jr $ra # return 0;
.data
prompt1_msg:
.asciiz "Enter a number: "
prompt2_msg:
.asciiz "Enter another number: "
result_msg_1:
.asciiz "The sum of the squares of "
result_msg_2:
.asciiz " and "
result_msg_3:
.asciiz " is "
Print a message only if a number is even.
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("even\n");
}
return 0;
}
Print a message only if a number is even.
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 != 0) goto epilogue;
printf("even\n");
epilogue:
return 0;
}
Calculate 1*1 + 2*2 + ... + 99*99 + 100*100
#include <stdio.h>
int main(void) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i * i;
}
printf("%d\n", sum);
return 0;
}
Calculate 1*1 + 2*2 + ... + 99*99 + 100*100.
#define UPPER_BOUND 100
#include <stdio.h>
int main(void) {
int sum = 0;
loop_i_to_100__init:;
int i = 0;
loop_i_to_100__cond:
if (i > UPPER_BOUND) goto loop_i_to_100__end;
loop_i_to_100__body:
sum += i * i;
loop_i_to_100__step:
i++;
goto loop_i_to_100__cond;
loop_i_to_100__end:
printf("%d", sum);
putchar('\n');
return 0;
}
Calculate 1*1 + 2*2 + ... + 99*99 + 100*100
UPPER_BOUND = 100
.text
main:
# Locals:
# - $t0: int sum
# - $t1: int i
# - $t2: temporary value
li $t0, 0 # int sum = 0;
loop_i_to_100__init:
li $t1, 1 # int i = 0;
loop_i_to_100__cond:
bgt $t1, UPPER_BOUND, loop_i_to_100__end # while (i < UPPER_BOUND) {
loop_i_to_100__body:
mul $t2, $t1, $t1 # sum = (i * i) +
add $t0, $t0, $t2 # sum;
loop_i_to_100__step:
addi $t0, $t0, 1 # i++;
b loop_i_to_100__cond # }
loop_i_to_100__end:
li $v0, 1 # syscall 1: print_int
move $a0, $t0 #
syscall # printf("%d", sum);
li $v0, 11 # syscall 11: print_char
li $a0, '\n' #
syscall # putchar('\n');
li $v0, 0
jr $ra # return 0;
- 18s2 COMP1521 Lecture Video: Data representation: floats, arrays, structs
- Wikpedia: floating point representation
- Floating point calculator
hello world implemented with a direct syscall
This isn't portable or readable but shows us what system calls look like.
This isn't portable or readable but shows us what system calls look like.
#include <unistd.h>
int main(void) {
char bytes[13] = "Hello, Zac!\n";
// argument 1 to syscall is the system call number, 1 is write
// remaining arguments are specific to each system call
// write system call takes 3 arguments:
// 1) file descriptor, 1 == stdout
// 2) memory address of first byte to write
// 3) number of bytes to write
syscall(1, 1, bytes, 12); // prints Hello, Zac! on stdout
return 0;
}
#include <unistd.h>
#define O_RDONLY 00
#define O_WRONLY 01
#define O_CREAT 0100
#define O_TRUNC 01000
// cp <file1> <file2> with syscalls and no error handling
int main(int argc, char *argv[]) {
// system call number 2 is open, takes 3 arguments:
// 1) address of zero-terminated string containing file pathname
// 2) bitmap indicating whether to write, read, ... file
// O_WRONLY | O_CREAT == 0x41 == write to file, creating if necessary
// 3) permissions if file will be newly created
// 0644 == readable to everyone, writeable by owner
long read_file_descriptor = syscall(2, argv[1], O_RDONLY, 0);
long write_file_descriptor = syscall(2, argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
while (1) {
// system call number 0 is read - takes 3 arguments:
// 1) file descriptor
// 2) memory address to put bytes read
// 3) maximum number of bytes read
// returns number of bytes actually read
char bytes[4096];
long bytes_read = syscall(0, read_file_descriptor, bytes, 4096);
if (bytes_read <= 0) {
break;
}
// system call number 1 is write - takes 3 arguments:
// 1) file descriptor
// 2) memory address to take bytes from
// 3) number of bytes to written
// returns number of bytes actually written
syscall(1, write_file_descriptor, bytes, bytes_read);
}
return 0;
}
hello world implemented with libc
#include <unistd.h>
int main(void) {
char bytes[13] = "Hello, Zac!\n";
// write takes 3 arguments:
// 1) file descriptor, 1 == stdout
// 2) memory address of first byte to write
// 3) number of bytes to write
write(1, bytes, 12); // prints Hello, Zac! on stdout
return 0;
}
cp <file1> <file2>
implemented with libc and *zero* error handling
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
// copy bytes one at a time from pathname passed as
// command-line argument 1 to pathname given as argument 2
int read_file_descriptor = open(argv[1], O_RDONLY);
int write_file_descriptor = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
while (1) {
char bytes[1];
ssize_t bytes_read = read(read_file_descriptor, bytes, 1);
if (bytes_read <= 0) {
break;
}
write(write_file_descriptor, bytes, 1);
}
return 0;
}
6 ways to print Hello, stdio!
#include <stdio.h>
int main(void) {
char bytes[] = "Hello, stdio!\n"; // 15 bytes
// write 14 bytes so we don't write (terminating) 0 byte
for (int i = 0; i < (sizeof bytes) - 1; i++) {
fputc(bytes[i], stdout);
}
// or as we know bytes is 0-terminated
for (int i = 0; bytes[i] != '\0'; i++) {
fputc(bytes[i], stdout);
}
// or if you prefer pointers
for (char *p = &bytes[0]; *p != '\0'; p++) {
fputc(*p, stdout);
}
// fputs relies on bytes being 0-terminated
fputs(bytes, stdout);
// write 14 1 byte items
fwrite(bytes, 1, (sizeof bytes) - 1, stdout);
// %s relies on bytes being 0-terminated
fprintf(stdout, "%s", bytes);
return 0;
}
cp <file1> <file2>
implemented with fgetc
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <source file> <destination file>\n", argv[0]);
return 1;
}
FILE *input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror(argv[1]); // prints why the open failed
return 1;
}
FILE *output_stream = fopen(argv[2], "w");
if (output_stream == NULL) {
perror(argv[2]);
return 1;
}
int c; // not char!
while ((c = fgetc(input_stream)) != EOF) {
fputc(c, output_stream);
}
fclose(input_stream); // optional here as fclose occurs
fclose(output_stream); // automatically on exit
return 0;
}
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// cp <file1> <file2> implemented with libc and no error handling
int main(int argc, char *argv[]) {
// open takes 3 arguments:
// 1) address of zero-terminated string containing pathname of file to open
// 2) bitmap indicating whether to write, read, ... file
// 3) permissions if file will be newly created
// 0644 == readable to everyone, writeable by owner
int read_file_descriptor = open(argv[1], O_RDONLY);
int write_file_descriptor = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
while (1) {
// read takes 3 arguments:
// 1) file descriptor
// 2) memory address to put bytes read
// 3) maximum number of bytes read
// returns number of bytes actually read
char bytes[4096];
ssize_t bytes_read = read(read_file_descriptor, bytes, 4096);
if (bytes_read <= 0) {
break;
}
// write takes 3 arguments:
// 1) file descriptor
// 2) memory address to take bytes from
// 3) number of bytes to written
// returns number of bytes actually written
write(write_file_descriptor, bytes, bytes_read);
}
// good practice to close file descriptions as soon as finished using them
// not necessary needed here as program about to exit
close(read_file_descriptor);
close(write_file_descriptor);
return 0;
}
use fseek to access diferent bytes of a file with no error checking
the return value of the calls to fopen, fseek and fgetc should be checked to see if they worked!
the return value of the calls to fopen, fseek and fgetc should be checked to see if they worked!
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <source file>\n", argv[0]);
return 1;
}
FILE *input_stream = fopen(argv[1], "rb");
// move to a position 1 byte from end of file
// then read 1 byte
fseek(input_stream, -1, SEEK_END);
printf("last byte of the file is 0x%02x\n", fgetc(input_stream));
// move to a position 0 bytes from start of file
// then read 1 byte
fseek(input_stream, 0, SEEK_SET);
printf("first byte of the file is 0x%02x\n", fgetc(input_stream));
// move to a position 41 bytes from start of file
// then read 1 byte
fseek(input_stream, 41, SEEK_SET);
printf("42nd byte of the file is 0x%02x\n", fgetc(input_stream));
// move to a position 58 bytes from current position
// then read 1 byte
fseek(input_stream, 58, SEEK_CUR);
printf("100th byte of the file is 0x%02x\n", fgetc(input_stream));
return 0;
}
use fseek to change a random bit in a file supplied as a command-line argument
for simplicty no error checking is done
good code would check the return values of the calls to fopen, fseek, fgetc, fputc
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <source file>\n", argv[0]);
return 1;
}
FILE *f = fopen(argv[1], "r+"); // open for reading and writing
fseek(f, 0, SEEK_END); // move to end of file
long n_bytes = ftell(f); // get number of bytes in file
srandom(time(NULL)); // initialize random number
// generator with current time
long target_byte = random() % n_bytes; // pick a random byte
fseek(f, target_byte, SEEK_SET); // move to byte
int byte = fgetc(f); // read byte
int bit = random() % 8; // pick a random bit
int new_byte = byte ^ (1 << bit); // flip the bit
fseek(f, -1, SEEK_CUR); // move back to same position
fputc(new_byte, f); // write the byte
fclose(f);
printf("Changed byte %ld of %s from 0x%02x to 0x%02x\n",target_byte, argv[1], byte, new_byte);
return 0;
}
call stat on each command line argument as simple example of its use
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
void stat_file(char *pathname);
int main(int argc, char *argv[]) {
for (int arg = 1; arg < argc; arg++) {
stat_file(argv[arg]);
}
return 0;
}
void stat_file(char *pathname) {
printf("stat(\"%s\", &s)\n", pathname);
struct stat s;
if (stat(pathname, &s) != 0) {
perror(pathname);
exit(1);
}
printf("ino = %10ld # Inode number\n", s.st_ino);
printf("mode = %10o # File mode \n", s.st_mode);
printf("nlink =%10ld # Link count \n", (long)s.st_nlink);
printf("uid = %10u # Owner uid\n", s.st_uid);
printf("gid = %10u # Group gid\n", s.st_gid);
printf("size = %10ld # File size (bytes)\n", (long)s.st_size);
printf("mtime =%10ld # Modification time (seconds since 1/1/70)\n",
(long)s.st_mtime);
}
$ dcc mkdir.c $ ./a.out new_dir $ ls -ld new_dir drwxr-xr-x 2 z5555555 z5555555 60 Oct 29 16:28 new_dir $
#include <stdio.h>
#include <sys/stat.h>
// create the directories specified as command-line arguments
int main(int argc, char *argv[]) {
for (int arg = 1; arg < argc; arg++) {
if (mkdir(argv[arg], 0755) != 0) {
perror(argv[arg]); // prints why the mkdir failed
return 1;
}
}
return 0;
}
$ dcc list_directory.c $ ./a.out . list_directory.c a.out . .. $
#include <stdio.h>
#include <dirent.h>
// list the contents of directories specified as command-line arguments
int main(int argc, char *argv[]) {
for (int arg = 1; arg < argc; arg++) {
DIR *dirp = opendir(argv[arg]);
if (dirp == NULL) {
perror(argv[arg]); // prints why the open failed
return 1;
}
struct dirent *de;
while ((de = readdir(dirp)) != NULL) {
printf("%ld %s\n", de->d_ino, de->d_name);
}
closedir(dirp);
}
return 0;
}
$ dcc chmod.c $ ls -l chmod.c -rw-r--r-- 1 z5555555 z5555555 746 Nov 4 08:20 chmod.c $ ./a.out 600 chmod.c $ ls -l chmod.c -rw------- 1 z5555555 z5555555 787 Nov 4 08:22 chmod.c $ ./a.out 755 chmod.c chmod.c 755 $ ls -l chmod.c -rwxr-xr-x 1 z5555555 z5555555 787 Nov 4 08:22 chmod.c $
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
// change permissions of the specified files
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <mode> <files>\n", argv[0]);
return 1;
}
char *end;
// first argument is mode in octal
mode_t mode = strtol(argv[1], &end, 8);
// check first argument was a valid octal number
if (argv[1][0] == '\0' || end[0] != '\0') {
fprintf(stderr, "%s: invalid mode: %s\n", argv[0], argv[1]);
return 1;
}
for (int arg = 2; arg < argc; arg++) {
if (chmod(argv[arg], mode) != 0) {
perror(argv[arg]); // prints why the chmod failed
return 1;
}
}
return 0;
}
$ dcc rm.c $ ./a.out rm.c $ ls -l rm.c ls: cannot access 'rm.c': No such file or directory $
#include <stdio.h>
#include <unistd.h>
// remove the specified files
int main(int argc, char *argv[]) {
for (int arg = 1; arg < argc; arg++) {
if (unlink(argv[arg]) != 0) {
perror(argv[arg]); // prints why the unlink failed
return 1;
}
}
return 0;
}
broken attempt to implement cd
chdir() affects only this process and any it runs
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc > 1 && chdir(argv[1]) != 0) {
perror("chdir");
return 1;
}
return 0;
}
getcwd and chdir example
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
int main(void) {
// use repeated chdir("..") to climb to root of the file system
char pathname[PATH_MAX];
while (1) {
if (getcwd(pathname, sizeof pathname) == NULL) {
perror("getcwd");
return 1;
}
printf("getcwd() returned %s\n", pathname);
if (strcmp(pathname, "/") == 0) {
return 0;
}
if (chdir("..") != 0) {
perror("chdir");
return 1;
}
}
return 0;
}
$ dcc rename.c $ ./a.out rename.c renamed.c $ ls -l renamed.c renamed.c $
#include <stdio.h>
// rename the specified file
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <old-filename> <new-filename>\n",
argv[0]);
return 1;
}
char *old_filename = argv[1];
char *new_filename = argv[2];
if (rename(old_filename, new_filename) != 0) {
fprintf(stderr, "%s rename %s %s:", argv[0], old_filename,
new_filename);
perror("");
return 1;
}
return 0;
}
silly program which creates a 1000-deep directory hierarchy
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
int main(void) {
for (int i = 0; i < 1000;i++) {
char dirname[256];
snprintf(dirname, sizeof dirname, "d%d", i);
if (mkdir(dirname, 0755) != 0) {
perror(dirname);
return 1;
}
if (chdir(dirname) != 0) {
perror(dirname);
return 1;
}
char pathname[1000000];
if (getcwd(pathname, sizeof pathname) == NULL) {
perror("getcwd");
return 1;
}
printf("\nCurrent directory now: %s\n", pathname);
}
return 0;
}
silly program which create a 1000 links to file
in effect there are 1001 names for the file
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#include <string.h>
int main(int argc, char *argv[]) {
char pathname[256] = "hello.txt";
// create a target file
FILE *f1;
if ((f1 = fopen(pathname, "w")) == NULL) {
perror(pathname);
return 1;
}
fprintf(f1, "Hello Andrew!\n");
fclose(f1);
for (int i = 0; i < 1000; i++) {
printf("Verifying '%s' contains: ", pathname);
FILE *f2;
if ((f2 = fopen(pathname, "r")) == NULL) {
perror(pathname);
return 1;
}
int c;
while ((c = fgetc(f2)) != EOF) {
fputc(c, stdout);
}
fclose(f2);
char new_pathname[256];
snprintf(new_pathname, sizeof new_pathname,
"hello_%d.txt", i);
printf("Creating a link %s -> %s\n",
new_pathname, pathname);
if (link(pathname, new_pathname) != 0) {
perror(pathname);
return 1;
}
}
return 0;
}
$ dcc exec.c $ a.out good-bye cruel world $
#include <stdio.h>
#include <unistd.h>
// simple example of program replacing itself with exec
int main(void) {
char *echo_argv[] = {"/bin/echo","good-bye","cruel","world",NULL};
execv("/bin/echo", echo_argv);
// if we get here there has been an error
perror("execv");
return 1;
}
$ dcc fork.c $ a.out
I am the parent because fork() returned 2884551.
I am the child because fork() returned 0. $
#include <stdio.h>
#include <unistd.h>
int main(void) {
// fork creates 2 identical copies of program
// only return value is different
pid_t pid = fork();
if (pid == -1) {
perror("fork"); // print why the fork failed
} else if (pid == 0) {
printf("I am the child because fork() returned %d.\n", pid);
} else {
printf("I am the parent because fork() returned %d.\n", pid);
}
return 0;
}
simple example of classic fork/exec
run date --utc to print current UTC
use posix_spawn instead
use posix_spawn instead
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
int main(void) {
pid_t pid = fork();
if (pid == -1) {
perror("fork"); // print why fork failed
} else if (pid == 0) { // child
char *date_argv[] = {"/bin/date", "--utc", NULL};
execv("/bin/date", date_argv);
perror("execvpe"); // print why exec failed
} else { // parent
int exit_status;
if (waitpid(pid, &exit_status, 0) == -1) {
perror("waitpid");
exit(1);
}
printf("/bin/date exit status was %d\n", exit_status);
}
return 0;
}
simple example of system
#include <stdio.h>
#include <stdlib.h>
int main(void) {
// system passes string to a shell for evaluation
// brittle and highly vulnerable to security exploits
// system is suitable for quick debugging and throw-away programs only
// run date --utc to print current UTC
int exit_status = system("/bin/date --utc");
printf("/bin/date exit status was %d\n", exit_status);
return 0;
}
$ dcc spawn.c $ a.out
Tue 3 Nov 23:51:27 UTC 2022 /bin/date exit status was 0
simple example of posix_spawn run date --utc to print current UTC
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
#include <errno.h>
int main(void) {
pid_t pid;
extern char **environ;
char *date_argv[] = {"/bin/date", "--utc", NULL};
// spawn "/bin/date" as a separate process
int ret = posix_spawn(&pid, "/bin/date", NULL, NULL, date_argv, environ);
if (ret != 0) {
errno = ret; //posix_spawn returns error code, does not set errno
perror("spawn");
exit(1);
}
// wait for spawned processes to finish
int exit_status;
if (waitpid(pid, &exit_status, 0) == -1) {
perror("waitpid");
exit(1);
}
printf("/bin/date exit status was %d\n", exit_status);
return 0;
}
spawn ls -ld adding as argument the arguments we have been given
#include <stdio.h>
#include <stdlib.h>
#include <spawn.h>
#include <sys/wait.h>
#include <errno.h>
int main(int argc, char *argv[]) {
char *ls_argv[argc + 2];
ls_argv[0] = "/bin/ls";
ls_argv[1] = "-ld";
for (int i = 1; i <= argc; i++) {
ls_argv[i + 1] = argv[i];
}
pid_t pid;
extern char **environ;
int ret = posix_spawn(&pid, "/bin/ls", NULL, NULL, ls_argv, environ);
if (ret != 0) {
errno = ret;
perror("spawn");
return 1;
}
int exit_status;
if (waitpid(pid, &exit_status, 0) == -1) {
perror("waitpid");
exit(1);
}
// exit with whatever status ls exited with
return exit_status;
}
spawn ls -ld adding as argument the arguments we have been given
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *ls = "/bin/ls -ld";
int command_length = strlen(ls);
for (int i = 1; i < argc; i++) {
command_length += strlen(argv[i]) + 1;
}
// create command as string
char command[command_length + 1];
strcpy(command, ls);
for (int i = 1; i <= argc; i++) {
strcat(command, " ");
strcat(command, argv[i]);
}
int exit_status = system(command);
return exit_status;
}
print all environment variables
#include <stdio.h>
int main(void) {
// print all environment variables
extern char **environ;
for (int i = 0; environ[i] != NULL; i++) {
printf("%s\n", environ[i]);
}
}
simple example of using environment variableto change program behaviour
run date -to print time
Perth time printed, due to TZ environment variable
Perth time printed, due to TZ environment variable
#include <stdio.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
#include <errno.h>
int main(void) {
pid_t pid;
char *date_argv[] = { "/bin/date", NULL };
char *date_environment[] = { "TZ=Australia/Perth", NULL };
// print time in Perth
int ret = posix_spawn(&pid, "/bin/date", NULL, NULL, date_argv,
date_environment);
if (ret != 0) {
errno = ret;
perror("spawn");
return 1;
}
int exit_status;
if (waitpid(pid, &exit_status, 0) == -1) {
perror("waitpid");
return 1;
}
printf("/bin/date exit status was %d\n", exit_status);
return 0;
}
$ dcc get_status.c -o get_status $ STATUS=ok ./get_status
Environment variable 'STATUS' has value 'ok' $
#include <stdio.h>
#include <stdlib.h>
// simple example of accessing an environment variable
int main(void) {
// print value of environment variable STATUS
char *value = getenv("STATUS");
printf("Environment variable 'STATUS' has value '%s'\n", value);
return 0;
}
$ dcc set_status.c -o set_status $ dcc get_status.c -o get_status $ ./set_status
Environment variable 'STATUS' has value 'great' $
#include <stdio.h>
#include <stdlib.h>
#include <spawn.h>
#include <sys/wait.h>
#include <errno.h>
// simple example of setting an environment variable
int main(void) {
// set environment variable STATUS
setenv("STATUS", "great", 1);
char *getenv_argv[] = {"./get_status", NULL};
pid_t pid;
extern char **environ;
int ret = posix_spawn(&pid, "./get_status", NULL, NULL,
getenv_argv, environ);
if (ret != 0) {
errno = ret;
perror("spawn");
return 1;
}
int exit_status;
if (waitpid(pid, &exit_status, 0) == -1) {
perror("waitpid");
exit(1);
}
// exit with whatever status s exited with
return exit_status;
}
A simple example which launches two threads of execution.
$ gcc -pthread two_threads.c -o two_threads $ ./two_threads | more Hello this is thread #1 i=0 Hello this is thread #1 i=1 Hello this is thread #1 i=2 Hello this is thread #1 i=3 Hello this is thread #1 i=4 Hello this is thread #2 i=0 Hello this is thread #2 i=1 ...
#include <pthread.h>
#include <stdio.h>
// This function is called to start thread execution.
// It can be given any pointer as an argument.
void *run_thread(void *argument) {
int *p = argument;
for (int i = 0; i < 10; i++) {
printf("Hello this is thread #%d: i=%d\n", *p, i);
}
// A thread finishes when either the thread's start function
// returns, or the thread calls `pthread_exit(3)'.
// A thread can return a pointer of any type --- that pointer
// can be fetched via `pthread_join(3)'
return NULL;
}
int main(void) {
// Create two threads running the same task, but different inputs.
pthread_t thread_id1;
int thread_number1 = 1;
pthread_create(&thread_id1, NULL, run_thread, &thread_number1);
pthread_t thread_id2;
int thread_number2 = 2;
pthread_create(&thread_id2, NULL, run_thread, &thread_number2);
// Wait for the 2 threads to finish.
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
return 0;
}
Simple example of running an arbitrary number of threads.
For example::
$ gcc -pthread n_threads.c -o n_threads $ ./n_threads 10 Hello this is thread 0: i=0 Hello this is thread 0: i=1 Hello this is thread 0: i=2 Hello this is thread 0: i=3 Hello this is thread 0: i=4 Hello this is thread 0: i=5 Hello this is thread 0: i=6 Hello this is thread 0: i=7 ...
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *run_thread(void *argument) {
int *p = argument;
for (int i = 0; i < 42; i++) {
printf("Hello this is thread %d: i=%d\n", *p, i);
}
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <n-threads>\n", argv[0]);
return 1;
}
int n_threads = strtol(argv[1], NULL, 0);
assert(0 < n_threads && n_threads < 100);
pthread_t thread_id[n_threads];
int argument[n_threads];
for (int i = 0; i < n_threads; i++) {
argument[i] = i;
pthread_create(&thread_id[i], NULL, run_thread, &argument[i]);
}
// Wait for the threads to finish
for (int i = 0; i < n_threads; i++) {
pthread_join(thread_id[i], NULL);
}
return 0;
}
Simple example of dividing a task between `n' threads.
Compile like:
$ gcc -O3 -pthread thread_sum.c -o thread_sum
One thread takes 10 seconds:
$ time ./thread_sum 1 10000000000 Creating 1 threads to sum the first 10000000000 integers Each thread will sum 10000000000 integers Thread summing integers 0 to 10000000000 finished sum is 49999999990067863552
Combined sum of integers 0 to 10000000000 is 49999999990067863552
real 0m11.924s user 0m11.919s sys 0m0.004s $
Four threads runs 4x as fast on a machine with 4 cores:
$ time ./thread_sum 4 10000000000 Creating 4 threads to sum the first 10000000000 integers Each thread will sum 2500000000 integers Thread summing integers 2500000000 to 5000000000 finished sum is 9374999997502005248 Thread summing integers 7500000000 to 10000000000 finished sum is 21874999997502087168 Thread summing integers 5000000000 to 7500000000 finished sum is 15624999997500696576 Thread summing integers 0 to 2500000000 finished sum is 3124999997567081472
Combined sum of integers 0 to 10000000000 is 49999999990071869440
real 0m3.154s user 0m12.563s sys 0m0.004s $
Note the result is inexact, because we use values can't be exactly represented as double and exact value printed depends on how many threads we use - because we break up the computation differently depending on the number of threads.
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct job {
long start, finish;
double sum;
};
void *run_thread(void *argument) {
struct job *j = argument;
long start = j->start;
long finish = j->finish;
double sum = 0;
for (long i = start; i < finish; i++) {
sum += i;
}
j->sum = sum;
printf("Thread summing integers %10lu to %11lu finished sum is %20.0f\n",
start, finish, sum);
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <n-threads> <n-integers-to-sum>\n", argv[0]);
return 1;
}
int n_threads = strtol(argv[1], NULL, 0);
assert(0 < n_threads && n_threads < 1000);
long integers_to_sum = strtol(argv[2], NULL, 0);
assert(0 < integers_to_sum);
long integers_per_thread = (integers_to_sum - 1) / n_threads + 1;
printf("Creating %d threads to sum the first %lu integers\n"
"Each thread will sum %lu integers\n",
n_threads, integers_to_sum, integers_per_thread);
pthread_t thread_id[n_threads];
struct job jobs[n_threads];
for (int i = 0; i < n_threads; i++) {
jobs[i].start = i * integers_per_thread;
jobs[i].finish = jobs[i].start + integers_per_thread;
if (jobs[i].finish > integers_to_sum) {
jobs[i].finish = integers_to_sum;
}
// create a thread which will sum integers_per_thread integers
pthread_create(&thread_id[i], NULL, run_thread, &jobs[i]);
}
// Wait for threads to finish, then add results for an overall sum.
double overall_sum = 0;
for (int i = 0; i < n_threads; i++) {
pthread_join(thread_id[i], NULL);
overall_sum += jobs[i].sum;
}
printf("\nCombined sum of integers 0 to %lu is %.0f\n", integers_to_sum,
overall_sum);
return 0;
}
Simple example which launches two threads of execution, but which demonstrates the perils of accessing non-local variables from a thread.
$ gcc -pthread two_threads_broken.c -o two_threads_broken $ ./two_threads_broken|more Hello this is thread 2: i=0 Hello this is thread 2: i=1 Hello this is thread 2: i=2 Hello this is thread 2: i=3 Hello this is thread 2: i=4 Hello this is thread 2: i=5 Hello this is thread 2: i=6 Hello this is thread 2: i=7 Hello this is thread 2: i=8 Hello this is thread 2: i=9 Hello this is thread 2: i=0 Hello this is thread 2: i=1 Hello this is thread 2: i=2 Hello this is thread 2: i=3 Hello this is thread 2: i=4 Hello this is thread 2: i=5 Hello this is thread 2: i=6 Hello this is thread 2: i=7 Hello this is thread 2: i=8 Hello this is thread 2: i=9 $...
#include <pthread.h>
#include <stdio.h>
void *run_thread(void *argument) {
int *p = argument;
for (int i = 0; i < 10; i++) {
// variable thread_number will probably have changed in main
// before execution reaches here
printf("Hello this is thread %d: i=%d\n", *p, i);
}
return NULL;
}
int main(void) {
pthread_t thread_id1;
int thread_number = 1;
pthread_create(&thread_id1, NULL, run_thread, &thread_number);
thread_number = 2;
pthread_t thread_id2;
pthread_create(&thread_id2, NULL, run_thread, &thread_number);
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
return 0;
}
Simple example demonstrating unsafe access to a global variable from threads.
$ gcc -O3 -pthread bank_account_broken.c -o bank_account_broken $ ./bank_account_broken Andrew's bank account has $108829 $
#define _POSIX_C_SOURCE 199309L
#include <pthread.h>
#include <stdio.h>
#include <time.h>
int bank_account = 0;
// add $1 to Andrew's bank account 100,000 times
void *add_100000(void *argument) {
for (int i = 0; i < 100000; i++) {
// execution may switch threads in middle of assignment
// between load of variable value
// and store of new variable value
// changes other thread makes to variable will be lost
nanosleep(&(struct timespec){ .tv_nsec = 1 }, NULL);
// RECALL: shorthand for `bank_account = bank_account + 1`
bank_account++;
}
return NULL;
}
int main(void) {
// create two threads performing the same task
pthread_t thread_id1;
pthread_create(&thread_id1, NULL, add_100000, NULL);
pthread_t thread_id2;
pthread_create(&thread_id2, NULL, add_100000, NULL);
// wait for the 2 threads to finish
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
// will probably be much less than $200000
printf("Andrew's bank account has $%d\n", bank_account);
return 0;
}
Simple example demonstrating safe access to a global variable from threads, using a mutex (mutual exclusion) lock
$ gcc -O3 -pthread bank_account_mutex.c -o bank_account_mutex $ ./bank_account_mutex Andrew's bank account has $200000 $
#include <pthread.h>
#include <stdio.h>
int bank_account = 0;
pthread_mutex_t bank_account_lock = PTHREAD_MUTEX_INITIALIZER;
// add $1 to Andrew's bank account 100,000 times
void *add_100000(void *argument) {
for (int i = 0; i < 100000; i++) {
pthread_mutex_lock(&bank_account_lock);
// only one thread can execute this section of code at any time
bank_account = bank_account + 1;
pthread_mutex_unlock(&bank_account_lock);
}
return NULL;
}
int main(void) {
// create two threads performing the same task
pthread_t thread_id1;
pthread_create(&thread_id1, NULL, add_100000, NULL);
pthread_t thread_id2;
pthread_create(&thread_id2, NULL, add_100000, NULL);
// wait for the 2 threads to finish
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
// will always be $200000
printf("Andrew's bank account has $%d\n", bank_account);
return 0;
}
! simple example which launches two threads of execution
! which increment a global variable
#include <pthread.h>
#include <stdio.h>
int andrews_bank_account = 200;
pthread_mutex_t andrews_bank_account_lock = PTHREAD_MUTEX_INITIALIZER;
int xaviers_bank_account = 100;
pthread_mutex_t xaviers_bank_account_lock = PTHREAD_MUTEX_INITIALIZER;
// Andrew sends Xavier all his money dollar by dollar
void *andrew_send_xavier_money(void *argument) {
for (int i = 0; i < 100000; i++) {
pthread_mutex_lock(&andrews_bank_account_lock);
pthread_mutex_lock(&xaviers_bank_account_lock);
if (andrews_bank_account > 0) {
andrews_bank_account--;
xaviers_bank_account++;
}
pthread_mutex_unlock(&xaviers_bank_account_lock);
pthread_mutex_unlock(&andrews_bank_account_lock);
}
return NULL;
}
// Xavier sends Andrew all his money dollar by dollar
void *xavier_send_andrew_money(void *argument) {
for (int i = 0; i < 100000; i++) {
pthread_mutex_lock(&xaviers_bank_account_lock);
pthread_mutex_lock(&andrews_bank_account_lock);
if (xaviers_bank_account > 0) {
xaviers_bank_account--;
andrews_bank_account++;
}
pthread_mutex_unlock(&andrews_bank_account_lock);
pthread_mutex_unlock(&xaviers_bank_account_lock);
}
return NULL;
}
int main(void) {
// create two threads sending each other money
pthread_t thread_id1;
pthread_create(&thread_id1, NULL, andrew_send_xavier_money, NULL);
pthread_t thread_id2;
pthread_create(&thread_id2, NULL, xavier_send_andrew_money, NULL);
// threads will probably never finish
// deadlock will likely likely occur
// with one thread holding andrews_bank_account_lock
// and waiting for xaviers_bank_account_lock
// and the other thread holding xaviers_bank_account_lock
// and waiting for andrews_bank_account_lock
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
return 0;
}
All Links
- All Tutorial Questions
- All Tutorial Answers
- All Laboratory Exercises
- All Laboratory Sample Solutions
- All Weekly Test Questions
- All Weekly Test Sample Answers
- All Extra Revision Revision Questions
- All Extra Revision Sample Solutions
- Mips Basics
- Mips Control
- Mips Data
- Mips Functions
- Integers
- Bitwise Operations
- Floating Point
- Files
- Unicode
- Processes
- Threads
- Virtual Memory
Square two numbers and sum their squares.