Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OSL 10: ONE WAY COMMUNICATION B/W TWO PROCESSES
- - sender:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- int main()
- {
- int fd;
- char * myfifo = "/tmp/myfifo";
- mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
- char message[100];
- printf("Enter message: ");
- fgets(message, 100, stdin);
- fd = open(myfifo, O_WRONLY); // open the FIFO for writing
- write(fd, message, strlen(message)+1); // write the message to the FIFO
- close(fd);
- unlink(myfifo); // remove the FIFO
- return 0;
- }
- - receiver:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- int main()
- {
- int fd;
- char * myfifo = "/tmp/myfifo";
- mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
- char message[100];
- fd = open(myfifo, O_RDONLY); // open the FIFO for reading
- read(fd, message, 100); // read the message from the FIFO
- printf("Received message: %s\n", message);
- close(fd);
- unlink(myfifo); // remove the FIFO
- return 0;
- }
- --------------------------------------------------------------------------------------------------------------------
- OSL 11: TWO WAY COMMUNICATION B/W TWO PROCESSES
- sender.c -
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main()
- {
- int fd;
- char *fifo = "/tmp/myfifo";
- mkfifo(fifo, 0666);
- char arr1[80], arr2[80];
- while (1)
- {
- printf("Waiting for input from User2...\n");
- fd = open(fifo, O_WRONLY);
- printf("User1: ");
- fgets(arr2, 80, stdin);
- write(fd, arr2, sizeof(arr2));
- close(fd);
- fd = open(fifo, O_RDONLY);
- read(fd, arr1, sizeof(arr1));
- printf("User2: %s\n", arr1);
- close(fd);
- }
- return 0;
- }
- receiver.c -
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main()
- {
- int fd;
- char *fifo = "/tmp/myfifo";
- mkfifo(fifo, 0666);
- char arr1[80], arr2[80];
- while (1)
- {
- printf("Waiting for input from User1...\n");
- fd = open(fifo, O_RDONLY);
- read(fd, arr1, sizeof(arr1));
- printf("User1: %s\n", arr1);
- close(fd);
- fd = open(fifo, O_WRONLY);
- printf("User2: ");
- fgets(arr2, 80, stdin);
- write(fd, arr2, sizeof(arr2));
- close(fd);
- }
- return 0;
- }
- ------------------------------------------------------------------------------------------------------------------------
- OSL 12: DEMONSTRATE WAIT AND SLEEP SYSTEM CALLS
- sleep():
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main() {
- printf("Before sleep\n");
- sleep(5); // sleep for 5 seconds
- printf("After sleep\n");
- return 0;
- }
- wait():
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- int main() {
- pid_t pid;
- pid = fork();
- if (pid == 0) { // child process
- printf("Child process is running\n");
- sleep(5); // child process sleeps for 5 seconds
- exit(0);
- } else if (pid > 0) { // parent process
- printf("Parent process is waiting for child process to terminate\n");
- wait(NULL); // parent process waits for child process to terminate
- printf("Child process terminated\n");
- } else { // error
- fprintf(stderr, "Fork failed\n");
- exit(1);
- }
- return 0;
- }
- -----------------------------------------------------------------------------------------------------------------------
- OSL 13: BASIC THREAD OPERATIONS
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- void *print_hello(void *arg) {
- printf("Hello from thread %ld\n", (long)arg);
- pthread_exit(NULL);
- }
- int main() {
- pthread_t thread_id;
- int rc;
- // create a new thread
- rc = pthread_create(&thread_id, NULL, print_hello, (void *)1);
- if (rc) {
- printf("ERROR; return code from pthread_create() is %d\n", rc);
- exit(-1);
- }
- // wait for the thread to complete
- rc = pthread_join(thread_id, NULL);
- if (rc) {
- printf("ERROR; return code from pthread_join() is %d\n", rc);
- exit(-1);
- }
- printf("Thread %ld has completed\n", (long)thread_id);
- pthread_exit(NULL);
- }
- ------------------------------------------------
- OPTIONAL THREAD OPS:
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- pthread_mutex_t mutex;
- void *print_message(void *ptr);
- int main() {
- pthread_t thread1, thread2;
- char *message1 = "Thread 1";
- char *message2 = "Thread 2";
- int rc1, rc2;
- // initialize mutex
- pthread_mutex_init(&mutex, NULL);
- // create thread 1
- printf("Creating thread 1...\n");
- rc1 = pthread_create(&thread1, NULL, print_message, (void*) message1);
- if (rc1) {
- printf("Error: return code from pthread_create() is %d\n", rc1);
- exit(-1);
- }
- // detach thread 1
- printf("Detaching thread 1...\n");
- rc1 = pthread_detach(thread1);
- if (rc1) {
- printf("Error: return code from pthread_detach() is %d\n", rc1);
- exit(-1);
- }
- // create thread 2
- printf("Creating thread 2...\n");
- rc2 = pthread_create(&thread2, NULL, print_message, (void*) message2);
- if (rc2) {
- printf("Error: return code from pthread_create() is %d\n", rc2);
- exit(-1);
- }
- // join thread 2
- printf("Joining thread 2...\n");
- rc2 = pthread_join(thread2, NULL);
- if (rc2) {
- printf("Error: return code from pthread_join() is %d\n", rc2);
- exit(-1);
- }
- // lock mutex and print final message
- printf("Locking mutex...\n");
- pthread_mutex_lock(&mutex);
- printf("Main thread and thread 2 are done!\n");
- pthread_mutex_unlock(&mutex);
- // destroy mutex
- printf("Destroying mutex...\n");
- pthread_mutex_destroy(&mutex);
- printf("Main thread exiting...\n");
- pthread_exit(NULL);
- }
- void *print_message(void *ptr) {
- char *message = (char*) ptr;
- // lock mutex
- printf("Locking mutex...\n");
- pthread_mutex_lock(&mutex);
- printf("%s\n", message);
- // unlock mutex
- printf("Unlocking mutex...\n");
- pthread_mutex_unlock(&mutex);
- pthread_exit(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement