Experiments For Practice
Experiments For Practice
1. program showcases how to create child processes, wait for their completion, and
execute different commands within these processes using system calls like
fork (), exec () and wait ().
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
if (pid < 0) {
// Fork failed
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
printf("Child process (PID: %d), Parent PID: %d\n", getpid(), getppid());
if (WIFEXITED(status)) {
printf("Child process exited with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child process terminated abnormally\n");
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main() {
pthread_t thread_id; // Thread ID variable
if (result != 0) {
perror("Thread creation failed"); // Print an error message if thread creation fails
exit(EXIT_FAILURE);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 4
pthread_exit(NULL);
}
int main() {
pthread_t tid[NUM_THREADS];
struct ThreadData thread_data[NUM_THREADS];
int range_start = 1;
int range_end = 100;
int range_size = (range_end - range_start + 1) / NUM_THREADS;
int remainder = (range_end - range_start + 1) % NUM_THREADS;
return 0;
}
4. Program for multiple threads which concurrently compute and update a shared
variable using mutex locks for synchronization
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 4
#define ITERATIONS 1000000
int shared_variable = 0;
pthread_mutex_t mutex;
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
pthread_mutex_init(&mutex, NULL);
// Create threads
for (int i = 0; i < NUM_THREADS; i++) {
thread_ids[i] = i;
if (pthread_create(&threads[i], NULL, thread_function, &thread_ids[i]) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
pthread_mutex_destroy(&mutex);
printf("All threads have finished execution. Final value of shared variable: %d\n",
shared_variable);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_THREADS 2
int shared_resource = 0;
sem_t semaphore;
// Acquire semaphore
sem_wait(&semaphore);
// Critical section
printf("Thread %d: Entering critical section\n", thread_id);
shared_resource++;
printf("Thread %d: Shared resource updated to %d\n", thread_id, shared_resource);
printf("Thread %d: Exiting critical section\n", thread_id);
// Release semaphore
sem_post(&semaphore);
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
// Initialize semaphore
sem_init(&semaphore, 0, 1);
// Create threads
for (int i = 0; i < NUM_THREADS; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, thread_function, (void*)&thread_ids[i]);
}
// Join threads
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Destroy semaphore
sem_destroy(&semaphore);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREADS 2
int shared_resource = 0;
pthread_mutex_t mutex;
// Lock mutex
pthread_mutex_lock(&mutex);
// Critical section
printf("Thread %d: Entering critical section\n", thread_id);
shared_resource++;
printf("Thread %d: Shared resource updated to %d\n", thread_id, shared_resource);
printf("Thread %d: Exiting critical section\n", thread_id);
// Unlock mutex
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
// Initialize mutex
pthread_mutex_init(&mutex, NULL);
// Create threads
for (int i = 0; i < NUM_THREADS; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, thread_function, (void*)&thread_ids[i]);
}
// Join threads
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Destroy mutex
pthread_mutex_destroy(&mutex);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 4
#define NUM_INCREMENTS 1000000
int shared_variable = 0;
int main() {
pthread_t threads[NUM_THREADS];
int rc;
long t;
// Create threads
for (t = 0; t < NUM_THREADS; t++) {
rc = pthread_create(&threads[t], NULL, increment_function, NULL);
if (rc) {
fprintf(stderr, "Error: pthread_create() failed\n");
exit(EXIT_FAILURE);
}
}
// After all threads have finished, print the final value of the shared variable
printf("Final value of shared_variable: %d\n", shared_variable);
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main() {
int fd[2];
pid_t pid;
char message[] = "Hello, pipe!";
if (pipe(fd) == -1) {
perror("pipe");
return 1;
}
pid = fork();
if (pid == 0) {
// Child process
close(fd[0]); // Close unused read end
write(fd[1], message, sizeof(message));
close(fd[1]);
} else if (pid > 0) {
// Parent process
close(fd[1]); // Close unused write end
char buffer[100];
read(fd[0], buffer, sizeof(buffer));
printf("Received message: %s\n", buffer);
close(fd[0]);
} else {
perror("fork");
return 1;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main() {
key_t key = 1234;
int shmid;
char *shm_ptr;
shmdt(shm_ptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct message {
long msg_type;
char msg_text[MSG_SIZE];
};
int main() {
key_t key = 1234;
int msqid;
struct message msg;
msg.msg_type = 1;
sprintf(msg.msg_text, "Hello, message queue!");
return 0;
}
11. Program based on socket
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char message[] = "Hello, socket!";
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Sending message
send(new_socket, message, strlen(message), 0);
printf("Message sent: %s\n", message);
return 0;
}