How to write compile and execute C program in Linux O/S

preview_player
Показать описание
#LearningSchools #CS604 #VU
CS604 Assignment No 1 Fall 2018 Part 2
How to write compile and execute C program in Linux O/S Step By Step

Problem Statement
You are required to write a C program that will create three threads named as; Thread 1, Thread 2 and Thread 3. You are required to run these threads in parallel fashion and show output as shown in sample screenshots. Each thread will display its Process and Thread ID as well.

The threads will have the additional functionalities:

Thread 1 will display the personal information of student such as student name and id.
Thread 2 will display the course related information such as Course Code and Course Name.
Thread 3 will display the degree name and university name.

All such information should be passed as parameters to thread function and then display such information in each thread. Make sure that parent thread or main thread should wait until all other threads have finished their execution. Finally, all threads will be terminated.

You are required to Compile & run C program on Linux Operating system. You will compile your C program file with your Student ID.

Note: File name other than your Student ID will not get marks.
You will write code and paste following two screenshots in Microsoft Word file.
1) You will take screenshot of compiling command that you will issue at Linux Terminal.
2) Then you will run your executable and take its screenshot.

Sample Output for Program

1. Compilation Phase



2. Program Output



Note: You are required to upload only Microsoft Word file which contains C code and required screenshots for login and program output. File other than Microsoft Word will not be accepted.

PArt 1 Video
Рекомендации по теме
Комментарии
Автор

Code for CS604 Assignment (Fall 2018)
Code for the Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREADS 3
struct thread_data{
int parentID, th_ID;
char *name;
char *detail;
};
struct thread_data thread_array[NUM_THREADS];
void *MythreadFunc(void *value){
int id, parentid;
char *naam;
char *kaam;
struct thread_data *my_data;
sleep(1);
my_data = (struct thread_data *)value;
id = my_data -> th_ID;
parentid = my_data -> parentID;
naam = my_data -> name;
kaam = my_data -> detail;
printf("\nThread with ID :%ld ", pthread_self());
printf(" and Parent ID: %d", parentid);
printf(" created and started\n ");
printf("%s\n", naam);
printf("%s\n", kaam);
pthread_exit(NULL);
return NULL;
}
int main(){
pthread_t thread[NUM_THREADS];
int process_id, rc;
process_id = getpid();
printf("\n\nCurrent Process ID :%d ", process_id);
// Data of struct
thread_array[0].name =
thread_array[0].detail = "XYZ";
thread_array[0].parentID = process_id;
thread_array[0].th_ID = pthread_self();
thread_array[1].name = "CS604";
thread_array[1].detail = "Operating System";
thread_array[1].parentID = process_id;
thread_array[1].th_ID = pthread_self();
thread_array[2].name = "BSSE";
thread_array[2].detail = "Virtual University of Pakistan";
thread_array[2].parentID = process_id;
thread_array[2].th_ID = pthread_self();
// creating threads via loop
for(int i = 0; i < NUM_THREADS; i++){
rc = pthread_create(&thread[i], NULL, MythreadFunc, (void *)&thread_array[i]);
if(rc){
printf("ERROR: return code from pthread_create() is %d\n", rc);
exit(-1);
}
}// joining the threads to main thread so it can wait until all created threads can execute before main thread
for(int i = 0; i < NUM_THREADS; i++)
pthread_join(thread[i], NULL);
printf("\n\nThreads are going to be terminated one by one\n");
printf("\n All Threads exited safely...\n");
pthread_exit(NULL);
return EXIT_SUCCESS;
}

LearningSchools
Автор

if gcc not working run this command in terminal sudo apt-get install gcc

LearningSchools
Автор

Please tell me how to handle those error...?

zubairzaman
Автор

run kr k jo new file desktop pr banti ha wo ni bnn ri errors aya ra ha

mateenfatima
Автор

AOA SIR
mujhy issue aa rha ha run nhi ho rha program
new file gcc -pthread bc150403815 -o bc150403815 k baad file nho ho rhi create error aa rha ha

saboorfazal
join shbcf.ru