Final SE314L review
Final SE314L review
Displays a calendar.
Example: cal 2024 (Year) or cal 12 2024 (Specific month).
o echo:
./<output_name>
int x = 5;
int *ptr = &x;
printf("Value: %d\n", *ptr);
2. Command-Line Arguments:
o Example:
2. Key Concepts:
o Parent and Child Processes: Created using fork().
ps -u <username>
2. Orphan Processes:
o If a parent dies, the child is adopted by the init process.
3. Zombie Processes:
o A child process that has terminated but its parent hasn’t collected its exit code using
wait().
4. Terminology:
o Parent Process: The process that creates a child.
Lab 7: Threads
1. Key Points:
o Threads share global memory but not local memory.
3. Race Conditions:
o Occurs when multiple threads access shared resources simultaneously without
synchronization.
pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
2. Fixing Race Conditions:
o Use mutex locks to protect critical sections:
pthread_mutex_lock(&lock);
// Critical section
pthread_mutex_unlock(&lock);
Lab 9: Semaphores
1. Key Concepts:
o Semaphores are used for synchronization.
2. Initialization Example:
sem_t semaphore;
sem_init(&semaphore, 0, 1); // Binary semaphore
Study Tips:
1. Review all the codes in Lab 10 thoroughly.
2. Practice writing pseudocode for process and thread operations.
3. Understand the concepts of race conditions, mutex locks, and semaphores through examples.
4. Focus on how to interpret commands and code outputs for Labs 2, 3, and 5.
5. Memorize the purpose and usage of key system calls for file management in Lab 10.
Study Steps
1. Understand Concepts
Lab 2 (Linux Commands):
o Purpose of each command (date, cal, echo, banner, who, tty, man).
o Be ready to use man to look up specific commands and interpret the manual's
sections, such as:
Name: What the command does.
Syntax: How the command is structured.
Options: Flags that modify the behavior of the command.
Lab 3 (File Commands and Basic C):
o Practice basic file commands (pwd, ls, mkdir, cd).
Lab 7 (Threads):
o Understand how threads share global memory but not local memory.
Lab 9 (Semaphores):
o Understand semaphore operations (sem_init, sem_wait, sem_post).
o For example, in Lab 6 (Fork), try adding multiple fork() calls and track the processes
created.
5. Create new code:
o Write a program that combines multiple concepts, such as creating a file, writing to it,
and reading from it using system calls (Lab 10).
3. Solve Scenarios
Example Scenarios:
1. Lab 6:
o "A child process sleeps for 3 seconds while the parent terminates. What happens to
the child?"
Answer: It becomes an orphan and is adopted by the init process.
2. Lab 7:
o "Two threads are incrementing a shared variable without synchronization. What
happens?"
Answer: A race condition occurs, causing unpredictable results.
3. Lab 10:
o "Open a file, write 'Hello', and then read it back. Write pseudocode."
ls [OPTION]... [FILE]...
o Answer: The square brackets indicate optional arguments. The ls command can be used
alone or with options (e.g., -l for detailed listing).
Code Snippets:
1. What does this code do?
int fd = open("data.txt", O_RDWR | O_CREAT, 0666);
write(fd, "OS Lab", 6);
o Answer: Opens or creates data.txt with read/write permissions and writes "OS Lab"
into it.
2. What happens here?
pthread_create(&tid, NULL, &my_function, NULL);
o Answer: Creates a new thread, where tid is the thread ID, and my_function is the
function executed by the thread.
Thread Functions:
o pthread_create: Creates a thread.
b. Write the command to display the current date and time in the format: DD-MM-YYYY.
3. Manual Interpretation:
Your instructor asks you to check the man page of the mkdir command. What does the
following section mean?
mkdir [OPTION]... DIRECTORY...
-p, --parents no error if existing, make parent directories as needed
Write your explanation.
2. Commands:
a. Write the sequence of commands to:
o Create a directory named test_folder.
b. You are asked to list all files and folders in your current directory, including hidden files.
Write the command.
3. C Programming:
a. Write the steps to compile and execute the following C code:
#include<stdio.h>
int main() {
printf("Hello, OS Lab!\n");
return 0;
}
b. What will happen if you run the above code without compiling it first?
2. Pointers:
a. Write the output of the following code snippet:
int x = 10;
int *ptr = &x;
printf("Address of x: %p\n", &x);
printf("Value of x using pointer: %d\n", *ptr);
b. Modify the code above to update the value of x through the pointer ptr.
3. Command-Line Arguments:
Write a program that takes two numbers as command-line arguments and prints their sum.
Provide the steps to compile and run the program.
Lab 5: Process Creation
1. True or False:
a. The fork() function creates a new child process and assigns it a unique PID.
b. A parent process automatically waits for its child process to finish execution.
2. Process IDs:
a. Write a program to print the process ID (PID) of the current process and its parent process.
3. Finding Processes:
a. Write the command to display all processes running under the user os_student.
3. Pseudocode:
Write pseudocode to create a child process, make the parent process wait for the child to
complete, and then terminate both.
Lab 7: Threads
1. True or False:
a. Threads in the same process share global memory but not local memory.
2. Thread Creation:
Write a program that creates a thread to print "Hello from the thread!" and another thread to
print "Goodbye from the thread!". Ensure the main program waits for both threads to
complete.
3. Initialization:
Write the code to initialize a mutex lock in the main function.
Lab 9: Semaphores
1. True or False:
a. A semaphore with an initial value of 1 can only be accessed by one thread at a time.
2. Scenarios:
a. A semaphore is initialized to 0. What happens when a thread calls sem_wait on it?
b. Write a code snippet to initialize a semaphore with value 1, decrement it in one thread, and
increment it in another.
2. File Operations:
a. Write a program to:
o Create a file named test.txt.
3. Manual Interpretation:
o mkdir [OPTION]... DIRECTORY...: This means that the mkdir command creates a
directory. The -p flag ensures no error occurs if the directory exists, and it creates
parent directories as needed.
4. Commands:
o a. who or who -H displays logged-in users.
2. Commands:
o a. Commands:
mkdir test_folder
cd test_folder
pico test_file.txt
o b. ls -a lists all files, including hidden files.
3. C Programming:
o a. Steps:
1. Save the code in a file, e.g., hello.c.
2. Compile it: gcc hello.c -o hello.
3. Run it: ./hello.
o b. If you try to run it without compiling, the system will return an error indicating the
file is not executable.
2. Pointers:
o a. Output:
int x = 10;
int *ptr = &x;
*ptr = 20;
printf("Updated value of x: %d\n", x);
3. Command-Line Arguments:
o Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: program <num1> <num2>\n");
return 1;
}
int num1 = atoi(argv[1]);
int num2 = atoi(argv[2]);
printf("Sum: %d\n", num1 + num2);
return 0;
}
o Compile: gcc sum.c -o sum
o Run: ./sum 5 10
o b. False – A parent process continues execution unless explicitly waiting for the
child.
2. Process IDs:
o a. Program:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Current PID: %d\n", getpid());
printf("Parent PID: %d\n", getppid());
return 0;
}
o b. Explanation:
2. Scenarios:
o a. The child becomes an orphan and is adopted by the init process.
o b. Output:
Parent process.
Child process. (after 3 seconds)
3. Pseudocode:
fork()
if child:
print("Child process")
exit()
else:
wait()
print("Parent process")
Lab 7: Threads
1. True or False:
o a. True – Threads share global memory but not local memory.
2. Thread Creation:
o Code:
#include <pthread.h>
#include <stdio.h>
void *print_hello(void *arg) {
printf("%s\n", (char *)arg);
return NULL;
}
int main() {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, print_hello, "Hello from thread 1!");
pthread_create(&tid2, NULL, print_hello, "Goodbye from thread 2!");
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
Hello, Thread 1
Hello, Thread 2
(Order may vary due to thread scheduling.)
pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
void *increment(void *arg) {
for (int i = 0; i < 1000; i++) {
pthread_mutex_lock(&lock);
counter++;
pthread_mutex_unlock(&lock);
}
return NULL;
}
3. Initialization:
pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
Lab 9: Semaphores
1. True or False:
o a. True – A semaphore with an initial value of 1 allows one thread at a time.
2. Scenarios:
o a. If sem_wait is called on a semaphore initialized to 0, the calling thread blocks until
another thread increments it using sem_post.
o b. Code:
sem_t semaphore;
sem_init(&semaphore, 0, 1);
sem_wait(&semaphore);
// Critical section
sem_post(&semaphore);
o b. False – lseek moves the file pointer but doesn’t delete files.
2. File Operations:
o a. Program:
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int fd = open("test.txt", O_RDWR | O_CREAT, 0666);
write(fd, "Operating Systems", 17);
char buffer[20];
lseek(fd, 0, SEEK_SET);
read(fd, buffer, 17);
buffer[17] = '\0';
printf("%s\n", buffer);
close(fd);
return 0;
}
o b. Explanation: