Linux Internals Programming Assignments and Exercises

Home Linux Internals | Course Home Linux Internals | Assignments

Linux Internals and TCP/IP Networking – Assignments

Linux Internals Programming Assignments will ensure you apply the concepts you have learned in your classroom. By solving these assignments, you will go through a systematic problem-solving approach, which includes requirement understanding, algorithm design, pseudocode creation, dry run, and final execution. As you move from simple to more complex assignments of each module, it will slowly start building your self-confidence. Explore the additional dimension with hands-on learning through Linux Internals Exercises to further enhance your mastery.

Chapter 1 : System Calls usage in Linux

Prerequisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Command line arguments, File operation system calls (open, read, write, close, fstat ..etc)

Objective:

  • To understand and implement using basic system calls.

Requirements:

  1. Copy source file to destination file which passed through cmd-line arguments. After copying both files must have equal size, even it’s a zero sized file.
    Eg: – ./my_copy source.txt dest.txt.
  2. If arguments are missing show the usage (help) info to user.
  3. Implement a my_copy() function where you have to pass two file descriptors.
    Int my_copy(int source_fd, int dest_fd);
  4. If –p option passed copy permissions as well to destination (refer ‘fstat’ man page).
    Eg: – ./my_copy –p source.txt dest.txt.
  5. If the destination file is not present then create a new one with given name. Incase if it is present show a confirmation from user to overwrite.
    Eg: – ./my_copy source.txt destination.txt
            File “destination.txt” is already exists.
            Do you want to overwrite (Y/n)
  6. If user type ‘Y/n’ or enter key overwrite the destination with new file. In n/N don’t overwrite and exit from program.
  7. Program should able handle all possible error conditions.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments - Assignment 1/1

Test case 2

Linux Internals Programming Assignments - Assignment 1/2

Test case 3

Linux Internals Programming Assignments - Assignment 1/3

Test case 4

Linux Internals Programming Assignments - Assignment 1/4

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Command line arguments, File operation system calls (open, read, write, close ..etc)
  • Working of wc command

Objective:

  • To understand and implement using basic system calls.

Requirements:

  1. Count the number of words, lines and characters(bytes) from files passed through command line.
  2. If more than one files passed, print individual count values and file name + calculate the total of all values and print at the end.
  3. If no file are passed wc will read from std input till end of file(Ctrl + D) then count lines, words and characters(bytes).
  4. Implement a word_count() function where you have to pass fd and 3 integer addresses(pass by refference).                                                                                                          Int word_count (int fd, int *lines, int *words, int *bytes);
  5. Word_count function will read from the fd and calculates lines, words and bytes, then stores into respective addresses passed (don’t print values inside function).
  6. Main function will open the files in a loop and call word_count function depends upon number of files passed. Print values after calling functions in main.
    Eg: – ./word_count file1.txt file2.txt
    for(I = 0; I < 2; i++)
    {
    .
    .
             fd = open(…);
    word_count(fd, &lines, &words, &bytes);
    print_values(…);
    .
    .
    }
    Print_total(…);
  7. If options passed [ -l –w –c ] print only respective values.
  8. Program should able to handle all possible error conditions.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments - Assignment 2/1

Test case 2

Linux Internals Programming Assignments - Assignment 2/2

Test case 3

Test case 4

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Command line arguments, File operation system calls (open, read, write, close ..etc)
  • Working of dup system calls.

Objective:

  • To understand and implement using basic system calls.

Requirements:

  1. Using dup or dup2 redirect printf out to a given file instead of printing to stdout.
  2. Pass the file name using command-line arguments.
  3. Try using both system calls (dup and dup2).

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Command line arguments, File operation system calls (open, read, write, close ..etc)
  • Working of fcntl system calls.
  •  

Objective:

  • To understand and implement using advanced system calls.
  • Understand the need of file synchronization between processes.

Requirements:

  1. Using fcntl system call synchronize a file between two processes (parent and child process).
  2. Pass the file name using command-line arguments.
  3. Before writing to file check file is locked, in case it is locked must wait the process until its unlocked.
  4. If its unlocked, lock file and continue with writing.
  5. Both process will do the same procedure.

Chapter 2 : Threads

Pre-requisites:

  • Knowledge about multi-thread process, How to read and understand ‘man pages’.
  • Good knowledge about pthread library functions.

Objective:

  • To understand working and flow of multithread programs.

Requirements:

  1. Modify the factorial template code using multiple threads.
  2. Create at-least 3 threads and share the work among threads equally.
  3. Create and join threads using separate loops.
  4. You may have to change the argument structure.
  5. Declare all integer variables as unsigned long int (For max values).

Sample Execution / Output:

Test Case 1: 

Linux Internals Programming Assignments

Pre-requisites:

  • Knowledge about multi-thread process, How to read and understand ‘man pages’.
  • Good knowledge about pthread library functions.

Objective:

  • To understand working and flow of multithread programs.

Requirements:

  1. Modify the factorial template code using multiple threads.
  2. Create at-least 3 threads and share the work among threads equally.
  3. Create and join threads using separate loops.
  4. You may have to change the argument structure.
  5. Declare all integer variables as unsigned long int (For max values).

Sample Execution / Output:

Test Case 1: 

Pre-requisites:

  • Knowledge about multi-thread process, How to read and understand ‘man pages’.
  • Good knowledge about pthread library functions.
  • Multiplication of two matrices.
  • Dynamic allocation for 2D array.

Objective:

  • To understand working and flow of multithread programs..

Requirements:

  1. Create three local matrices, M1 MxN M2 NxP and Result MxP (M1 columns = M2 rows) where M, N & P values are provided by user.
  2. In case M1 columns != M2 rows print error message to user.
  3. Create all matrices using dynamic allocation.
  4. Use structure to pass arguments to threads
    sample structure.
    typedef struct thread_data
    {
    short m1_row;
    short m1_col;     short m2_col;int **matrix1;

    int **matrix2;

    int **result;

    short cur_row;

    }Thread_data_t;

  5. Each thread will calculate and store single row in result. So number of threads equals number of rows in M1.
    Eg:
  6. M1 =
     123
     111
     222

    M2 =

     111
     222
     333

    Thread1 → M1 row1 x M2 col1, col2, col3

     1×1 + 2×2 + 3×31×1 + 2×2 + 3×31×1 + 2×2 + 3×3
     141414

    Thread2 → M1 row2 x M2 col1, col2, col3

     1×1 + 1×2 + 1×31×1 + 1×2 + 1×31×1 + 1×2 + 1×3
     666

    Thread3 → M1 row3 x M2 col1, col2, col3

     2×1 + 2×2 + 2×32×1 + 2×2 + 2×32×1 + 2×2 + 2×3
     121212
  7. Don’t create any global variables.
  8. Create generic function for matrix dynamic allocation and deallocating.

Sample Execution / Output:

Test Case 1: 

Chapter 3 : Signals

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about signals and signal handling.
  • Working of sigaction system calls.

Objective:

  • To understand working of signal handling.

Requirements:

  • Write a signal handler function to print address which caused seg-fault(SIGSEGV).
  • Use sigaction system call to register signal.
  • Use struct siginfo from sa_sigaction to print address (Read man page).
  • Create a segmentation fault from code.
  • When error occurs program should print address and exit.

Sample Execution / Output:

Test case 1:

Linux Internals Programming Assignments - Assignment 4

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about signals and signal handlers.
  • Working of alarm system calls.

Objective:

  • To understand signals and time related system calls.

Requirements:

  1. User gives the time and date from command-line arguments.
  2. Validate the time eg: Do not go behind the current time.
  3. Date is an option, if user not providing date consider it as today.
  4. In handler, avoid all user communication(printf, scanf etc) and loops. Make it minimal as possible.
  5. After the alarm expires, display a ALARM message along with date and time.
  6. Prompt the user whether he wants to stop or snooze.
  7. If user selects stop, terminate the program.
  8. If user selects snooze, prompt for snooze time in minutes.
    > If user enters the time, reset the alarm to the entered time in minutes
    > If user doesn’t enter time, default the snooze time to 1 mins

Sample Execution / Output:

Test case 1Linux Internals Programming Assignments - Assignment 2/2

Test case 2

Linux Internals Programming Assignments - Assignment 2/3

Test case 3

Linux Internals Programming Assignments - Assignment 2/3

Test case 4

Linux Internals Programming Assignments - Assignment 2/4

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about signals and signal handling.
  • Working of sigaction system call and signal masking.

Objective:

  • To understand importance of signal masking.

Requirements:

  1. Write a signal handler function for any signal, say SIGINT .
  2. While its running inside handler (use loop) block other signals(atleast 3) being received.
  3. Use sa_mask from struct sigaction to set signals to be blocked (Refer man ).
  4. To create a signal set use variable of sigset_t.
  5. To add or remove signals from set use sigaddset, sigdelset functions (refer man).
  6. Process will receive blocked signals once its out from handler.

Sample Execution / Output:

Test case 1Linux Internals Programming Assignments - Assignment 3/1

Test case 2

Linux Internals Programming Assignments - Assignment 3/2

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about signals and signal handling.
  • Working of sigaction system call.

Objective:

  • To understand, how to avoid zombie asynchronously

Requirements:

  1. Write two separate programs for both methods.

    Method 1

    1. Create a child process.
    2. Write a signal handler function for SIGCHLD to avoid child become zombie (Do man 7 signal for SIGCHLD).
    3. Write code inside handler to avoid zombie and print child exit status.

    Method 2

    1. Create a child process.
    2. Use sa_flag from struct sigaction to avoid zombie (Refer man ).
    3. Prints the child exit status inside handler.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments - Assignment 4/2

Chapter 4 : Process

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about processes, zombie and orphan.
  • Working of fork system call.

Objective:

  • To understand different states of a process.

Requirements:

  1. Create a child process and print status that process is running
  2. After some time print status that process is zombie state
  3. After some time print zombie process cleared by init.
  4. To print status use help of /proc//status file.
    For eg: if child pid is 1234, open file /proc/1234/status and print first 3 lines
  5. If that file is not available means that process is cleared.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments - Assignment 5/1

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about processes & zombie process.
  • Working of fork & wait system call.

Objective:

  • To understand different states of a process.

Requirements:

  1. Create a child process avoid it become a zombie.
  2. To avoid zombie we need to call wait(), but this block parent until child terminates.
  3. So we need to use waitpid() with proper arguments (Read man page).
  4. When child is working parent has to continuously print some message.
  5. When ever child terminates parent has to print child terminated and print exit status of child process.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments - Assignment 5/2

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about processes.
  • Working of fork, wait and exec system calls.

Objective:

  • To understand how to use exec system calls.

Requirements:

  1. Create child and execute a command passed from command-line arguments.
  2. If no arguments passed print a usage message.
  3. In case any wrong command passed, print an error message.
  4. After child terminates print the exit status of child process.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments - Assignment 5/1

Test case 2Linux Internals Programming Assignments - Assignment 5/2

Test case 3

Linux Internals Programming Assignments - Assignment 5/3

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about processes.
  • Working of fork & wait system calls.

Objective:

  • To understand how to use fork system calls.

Requirements:

  1. Create three child process from same parent.
  2. Parent has to wait for all three child process.
  3. Print exit status of each child when they terminates.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments - Assignment 4/1

Chapter 5 : IPC

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about processes and IPC.
  • Working of pipe & dup system calls.

Objective:

  • To understand working of pipe between two process.

Requirements:

  1. Create two child process and execute commands passed from command-line arguments
  2. Each command is separated by a ‘|’ (pipe) character.
  3. First child execute first command (with or without options) and pass o/p to next.
  4. Second child executes second command (after ‘|’) will reads I/p from first cmd.
  5. Parent will wait for both child process to finish.

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments

Test case 2

Linux Internals Programming Assignments

Test case 3

Test case 4

Linux Internals Programming Assignments

Pre-requisites:

  • Knowledge about system calls, How to read and understand ‘man pages’.
  • Good knowledge about processes and IPC.
  • Working of pipe & dup system calls.

Objective:

  • To understand working of multiple pipes between multiple process.

Requirements:

  1. Create three child process and execute commands passed from command-line arguments
  2. Each command is separated by a ‘|’ (pipe) character.
  3. First child execute first command (ls -l) and pass output to next command.
  4. Second child executes second command (grep pattern) where pattern passed from command-line arguments.
  5. Third child executes wc -l

Sample Execution / Output:

Test case 1

Linux Internals Programming Assignments 2/1

Test case 2

Linux Internals Programming Assignments 2/2

Test case 3