0% found this document useful (0 votes)
3 views

Cheat

The final exam is scheduled for May 12, covering multiple choice, system design, code tracing, and programming related to Project 4. Key topics include process management, signal handling, threading, and network programming with practical examples provided. Allowed materials include handwritten notes, a calculator, and scratch paper.

Uploaded by

66zvffhzcb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cheat

The final exam is scheduled for May 12, covering multiple choice, system design, code tracing, and programming related to Project 4. Key topics include process management, signal handling, threading, and network programming with practical examples provided. Allowed materials include handwritten notes, a calculator, and scratch paper.

Uploaded by

66zvffhzcb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

# Final Exam Cheat Sheet

General Exam Details


- Date & Time: Monday, May 12, 8:00AM - 10:00AM
- Location: Totman Phys. Ed. Bldg. Gym
- Format:
- Part 1: Multiple Choice and Fill-in-the-Blank
- Part 2: System Design Choice
- Part 3 & 4: Code Tracing
- Part 5: Programming (related to Project 4)

- Allowed Materials:
- A4-size double-sided handwritten/printed notes.
- Calculator.
- Scratch paper provided.

---

PART 1: Multiple Choice and Fill-in-the-Blank

Processes
Zombie vs. Orphan Process
Zombie Process: A process that has terminated but whose parent hasn’t called wait().
Orphan Process: A child process whose parent has terminated.

Code Examples:
```c
// Zombie process
int main() {
if (fork() == 0) {
exit(0); // Child exits
} else {
while (1); // Parent doesn't call wait()
}
}

// Orphan process
int main() {
if (fork() == 0) {
while (1); // Child loops forever
} else {
exit(0); // Parent exits
}
}
```

Useful System Calls


- getpid(): Get current process ID.
- getppid(): Get parent process ID.
- fork(): Create a new process.
- wait(): Wait for a child process to finish.

---

Signals and Pipes

Signal Handling
Common Signals:
- SIGKILL: Terminates a process (cannot be caught or ignored).
- SIGSEGV: Sent when a process makes an illegal memory access.

Signal Workflow:
1. Signal received by process.
2. Control passes to the signal handler.
3. Signal handler executes.
4. Control returns to the next instruction.

```c
#include <signal.h>
#include <stdio.h>

void sig_handler(int signum) {


printf("Signal %d received\n", signum);
}

int main() {
signal(SIGINT, sig_handler); // Handle Ctrl+C
while (1);
}
```

Pipes
Parent-Child Communication:
- Use pipe().
- Close unused ends in each process.

Example:
```c
int main() {
int fd[2];
pipe(fd); // Create pipe
if (fork() == 0) {
close(fd[1]); // Close write end in child
char buf[100];
read(fd[0], buf, sizeof(buf));
printf("Child received: %s\n", buf);
} else {
close(fd[0]); // Close read end in parent
write(fd[1], "Hello, child!", 14);
}
return 0;
}
```

---

Threads and Synchronization

Mutex vs. Semaphore


- Mutex: Binary lock for exclusive access.
- Semaphore: Allows multiple resources to be accessed simultaneously.

```c
#include <pthread.h>
#include <semaphore.h>

sem_t semaphore;

void* thread_func(void* arg) {


sem_wait(&semaphore); // Wait
printf("Thread %d is accessing critical section\n", *((int*)arg));
sem_post(&semaphore); // Signal
return NULL;
}

int main() {
pthread_t threads[2];
sem_init(&semaphore, 0, 1); // Initialize with 1

int ids[2] = {1, 2};


pthread_create(&threads[0], NULL, thread_func, &ids[0]);
pthread_create(&threads[1], NULL, thread_func, &ids[1]);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);

sem_destroy(&semaphore);
return 0;
}
```

---

Network Programming

Socket API
Server Workflow:
1. socket()
2. bind()
3. listen()
4. accept()
5. recv()
6. send()
7. close()

Client Workflow:
1. socket()
2. connect()
3. send()
4. recv()
5. close()

Example: Simple Client-Server Communication


```c
// Server
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr = {AF_INET, htons(8080), INADDR_ANY};
bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
listen(sockfd, 5);
int client_fd = accept(sockfd, NULL, NULL);
char buffer[1024];
recv(client_fd, buffer, sizeof(buffer), 0);
printf("Received: %s\n", buffer);
send(client_fd, "Hello, client!", 14, 0);
close(client_fd);
close(sockfd);
// Client
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr = {AF_INET, htons(8080), inet_addr("127.0.0.1")};
connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
send(sockfd, "Hello, server!", 14, 0);
char buffer[1024];
recv(sockfd, buffer, sizeof(buffer), 0);
printf("Received: %s\n", buffer);
close(sockfd);
```

---

PART 2: System Design Choice

Design Questions: Focus on using processes, threads, signals, and shared resources efficiently.
Example:
- "How would you design a multi-threaded server to handle 1000 clients?"

---

PART 3 & 4: Code Tracing

Tips for Tracing:


1. Understand the flow of fork().
2. Trace variable states in threads.
3. Follow signal flow (e.g., SIGUSR1, SIGKILL).

---

PART 5: Programming

Project 4 Recap
Simulated Parallel Processing:
- Pipes for communication.
- Signals for task completion.

Example snippet for core process:


```c
while (read(pipe_read_fd, task, sizeof(task)) > 0) {
printf("Processing task: %s\n", task);
unsigned int result = (task_id >> (32 - n)) & ((1 << n) - 1);
write(pipe_write_fd, &result, sizeof(result));
kill(getppid(), SIGRTMIN);
}
```

Signal Handling in Main Process:


```c
struct sigaction sa = {0};
sa.sa_handler = handle_signal;
sigaction(SIGRTMIN, &sa, NULL);
```

---

Quick Reference:
- Common System Calls:
- fork(), wait(), exec(), pipe(), signal(), kill()
- Thread Functions:
- pthread_create(), pthread_join(), pthread_exit(), sem_wait(), sem_post()
- Socket API:
- socket(), bind(), listen(), accept(), connect(), recv(), send()

Practice code tracing and understand the core concepts! Good luck!

You might also like