Process and Threads

For this first assignment, you won’t be writing code; instead, you will be reading and executing the given below codes. Read the README instruction below for directions on how to compile and link the programs. Submit your answer in writing onto Blackboard.

Experiment 1. Thread and Process Creation
Study the programs

Don't use plagiarized sources. Get Your Custom Essay on
Process and Threads
Just from $13/Page
Order Essay

thr_create.c

and

fork.c

. These programs measure the average time to create a thread using thr_create() and to create a process using fork(). Compile and execute the programs. What do you conclude from this experiment? Describe the reasons for the difference in timings.
Experiment 2. Processes vs Threads
Study the two programs

thr_shared.c

and

proc_shared.c

. These programs are identical except that one uses threads and the other uses processes. Compile and execute the programs. What do you conclude from this second experiment? Explain the reason for the difference in behavior.
Experiment 3: The Update Problem
Study the programs

shared_data.c

and

SharedData.java

. In these examples, multiple threads are both updating (i.e., modifying) a shared variable. Compile and execute both. Are the programs behaving “correctly”? What do you conclude from this experiment?
README Instructions

  • Run the program on Linux platform.
  • To compile and run a C program (e.g., thr_create.c):
    gcc -o thr_create thr_create.c -lpthread thr_create
  • To build and run a classfile from the java code: javac SharedData.java
    java SharedData
  • Submission Guidelines and Requirements

    • Submit your answers for all the experiments as a PDF file onto Blackboard
    • Include your name, UCM ID and Certification statement in your solution:
      //Your name
      // Your UCM ID
      //Certificate of Authenticity: “I certify that the codes/answers of this assignment are entirely my own work.”

    thr_create.c

    #include #include #include #include main()

    {
    struct timeval start,end; long forktime;
    double avgtime; pthread_t last_thread; int i;
    int iters = 250;
    void *

    null_

    proc();

    gettimeofday(&start,NULL);

    for (i = 0; i < iters - 1; i++) /* create (iters-1) threads */ pthread_create(&last_thread,NULL,null_proc,NULL);

    pthread_create(&last_thread,NULL,null_proc,NULL); /* create last thread */ gettimeofday(&end,NULL);

    pthread_join(last_thread, NULL); /* wait for last thread */ forktime = (end.tv_sec – start.tv_sec)*1000000 +

    (end.tv_usec – start.tv_usec); avgtime = (double)forktime/(double)iters;

    printf(“Avg thr_create time = %f microsec\n”, avgtime); }

    void *null_proc() {}

    for (i = 0; i < iters; i++) /* create iters processes */ {

    if ((pid = fork()) == 0) /* child process */ {

    null_proc();

    exit(1); }

    else if (pid == -1) {

    /* error */

    printf(“error %d\n”,errno);

    exit(-1); }

    else /* parent process */ continue;

    }

    /* only parent process reaches this point */ gettimeofday(&end,NULL);
    for ( i = 0; i < iters; i++) /* have to do a wait for each child */ wait(&status); forktime = (end.tv_sec - start.tv_sec)*1000000 +

    (end.tv_usec – start.tv_usec); avgtime = (double)forktime/(double)iters;

    printf(“Avg fork time =%f microsec\n”, avgtime);

    } null_proc() {
    }

    fork.c

    #include #include #include

    main() {

    int pid;
    struct timeval start,end;
    int i;
    long forktime;
    double avgtime;
    int iters = 250;
    int status; gettimeofday(&start,NULL);

    #include #include #include #include #include

    void *proc();
    int shared_number;

    main() {

    int i;
    pthread_t new_thread; int sleep_time;
    int seed;

    shared_number = 1;

    printf(“Enter a positive integer for seed: “);
    scanf(“%d”,&seed);

    srand48(seed);

    /* initialize seed of random number stream */

    /* thr_create(NULL,0,proc,NULL,0,&new_thread);*/

    pthread_create(&new_thread,NULL,proc,NULL);

    /* create new thread */

    for (i = 0; i < 50; i++) {

    printf(“MAIN THREAD: i = %d, shared_number = %d\n”,i,shared_number); sleep_time = 100000.0*drand48(); /* generate random sleep time */ printf(“sleep time = %d microseconds\n”,sleep_time);

    usleep(sleep_time);

    shared_number = shared_number + 2; }

    pthread_join(new_thread,NULL);

    printf(“MAIN THREAD: DONE\n”); }

    thr_shared.c

    void *proc() {

    int i = 0; int DONE;

    DONE = 0; while (!DONE)

    { i++;

    if (i%10000 == 0)

    printf(“CHILD THREAD: i = %d,shared_number = %d\n”,i,shared_number);

    if (i > 5000000) DONE = 1;

    }
    printf(“CHILD THREAD: DONE\n”);

    }

    #include #include #include #include #include #include

    void *proc();
    int shared_number;
    main() {

    int i;
    pthread_t new_thread; int sleep_time;
    int pid;
    int status;
    int seed;

    shared_number = 1;
    proc_shared.c

    printf(“Enter a positive integer for seed: “); scanf(“%d”,&seed);

    srand48(seed);

    if ((pid = fork()) == 0) {

    proc();

    exit(0); }

    else if (pid == -1) {
    printf(“error %d\n”,errno);
    exit(-1); }

    else
    { /* parent process */

    for (i = 0; i < 50; i++) {

    /* initialize random number stream */ /* child process */

    /* error */

    printf(“MAIN PROCESS: i = %d, shared_number = %d\n”,i,shared_number); sleep_time = 100000.0*drand48(); /* generate random sleep time */ printf(“sleep time = %d microseconds\n”,sleep_time);
    usleep(sleep_time);

    shared_number = shared_number + 2; }

    wait(&status); /* wait for child process */

    printf(“MAIN PROCESS: DONE\n”); }

    }
    void *proc() {

    int i;
    int DONE;

    DONE = 0;
    i = 0;
    while (!DONE)

    { i++;
    if (i%10000 == 0)

    printf(“CHILD PROCESS: i = %d,shared_number = %d\n”,i,shared_number); if (i > 5000000)

    DONE = 1; }

    printf(“CHILD PROCESS: DONE\n”); }

    shared_data.c
    #include #include #include #include #include
    void *proc();
    int shared_number;
    main() {

    int i;
    pthread_t new_thread; int sleep_time;
    int number;
    int seed;

    shared_number = 1;
    printf(“Enter a positive integer for seed: “); scanf(“%d”,&seed);
    srand48(seed);

    pthread_create(&new_thread,NULL,proc,NULL);

    for (i = 0; i < 20; i++) {

    number = shared_number;
    printf(“MAIN THREAD: i = %d, shared_number = %d\n”,i,shared_number); sleep_time = 100000.0*drand48(); /* generate random sleep time */ printf(“sleep time = %d microseconds\n”,sleep_time);

    usleep(sleep_time);

    shared_number = number + 2; }

    pthread_join(new_thread,NULL);
    printf(“MAIN THREAD: DONE\n”); }
    void *proc() {

    int number,i; int sleep_time;

    printf(“CHILD\n”); for (i = 0; i < 10; i++)

    {
    number = shared_number;
    printf(“CHILD THREAD: i = %d, shared_number = %d\n”,i,shared_number); sleep_time = 100000.0*drand48(); /* generate random sleep time */ printf(“sleep time = %d microseconds\n”,sleep_time);
    usleep(sleep_time);
    shared_number = number + 200000;

    }
    printf(“CHILD THREAD: DONE\n”);
    }
    SharedData.java

    import java.util.Random;
    public class SharedData extends Thread {

    static Random gRandom = new Random(); static int gSharedNumber = 0;
    private int gID;
    public SharedData(int id) {

    this.gID = id; }

    public void run() { for(int i=0;i<20;i++) {

    int number = gSharedNumber; System.out.println(“SharedData[“+gID+”](i=”+i+”) = “+gSharedNumber); try { Thread.sleep(gRandom.nextInt(1000));
    } catch (InterruptedException e) { /* ignore */ }
    if(gID == 0) gSharedNumber = number + 1;
    else gSharedNumber = number + 1000;

    } }

    public static void main(String args[]) throws Exception { SharedData sd1 = new SharedData(0);
    SharedData sd2 = new SharedData(1);

    sd1.start(); sd2.start();

    sd1.join(); sd2.join(); }

    }

    AdvancedOperating Systems (CS 5500)

    Assignment 1 (60 points)

    Process and Threads

    Due by 17th February (Monday) 11:59pm

    You are allowed to discuss the problem and solution design with others, but the code/answer you

    submit must be your own. Your solution must include the certification of authenticity “I certify

    that the codes/answers of this assignment are

    entirely my own work.”

    For this first assignment, you won’t be writing code; instead, you will be reading and executing

    the given below codes. Read the README instruction below for directions on how to compile

    and link the programs. Submit your answer in writing onto Blackboard.

    • Experiment 1. Thread and Process Creation

    Study the programs thr_create.c and fork.c. These programs measure the average time to

    create a thread using thr_create() and to create a process using fork(). Compile and

    execute the programs. What do you conclude from this experiment? Describe the reasons

    for the difference in timings.

    • Experiment 2. Processes vs Threads

    Study the two programs thr_shared.c and proc_shared.c. These programs are identical

    except that one uses threads and the other uses processes. Compile and execute the

    programs. What do you conclude from this second experiment? Explain the reason for the

    difference in behavior.

    • Experiment 3: The Update Problem

    Study the programs shared_data.c and SharedData.java. In these examples, multiple

    threads are both updating (i.e., modifying) a shared variable. Compile and execute both.

    Are the programs behaving “correctly”? What do you conclude from this experiment?

    README Instructions

    • Run the program on Linux platform.

    • To compile and run a C program (e.g., thr_create.c):

    gcc -o thr_create thr_create.c -lpthread

    thr_create

    • To build and run a classfile from the java code:
    javac

    SharedData.java

    java SharedData

    Submission Guidelines and Requirements

    • Submit your answers for all the experiments as a PDF file onto Blackboard

    • Include your name, UCM ID and Certification statement in your solution:
    //Your name

    // Your UCM ID

    //Certificate of Authenticity: “I certify that the codes/answers of this assignment are

    entirely my own work.”

    thr_create.c

    #include

    #include

    #include

    #include

    main()

    {

    struct timeval start,end;

    long forktime;

    double avgtime;

    pthread_t last_thread;

    int i;

    int iters = 250;

    void *null_proc();

    gettimeofday(&start,NULL);

    for (i = 0; i < iters - 1; i++) /* create (iters-1) threads */

    pthread_create(&last_thread,NULL,null_proc,NULL);

    pthread_create(&last_thread,NULL,null_proc,NULL); /* create last thread */

    gettimeofday(&end,NULL);

    pthread_join(last_thread, NULL); /* wait for last thread */

    forktime = (end.tv_sec – start.tv_sec)*1000000 +

    (end.tv_usec – start.tv_usec);

    avgtime = (double)forktime/(double)iters;

    printf(“Avg thr_create time = %f microsec\n”, avgtime);

    }

    void *

    null_proc()

    {

    }

    https://cs.gmu.edu/~jchen/cs471/programming/thr_create.c

    fork.c

    #include
    #include
    #include

    main()

    {

    int pid;

    struct timeval start,end;
    int i;
    long forktime;
    double avgtime;
    int iters = 250;

    int status;

    gettimeofday(&start,NULL);

    for (i = 0; i < iters; i++) /* create iters processes */

    {

    if ((pid = fork()) == 0) /* child process */

    {

    null_proc();

    exit(1);

    }

    else if (pid == -1) /* error */

    {

    printf(“error %d\n”,errno);

    exit(-1);

    }

    else /* parent process */

    continue;

    }

    /* only parent process reaches this point */

    gettimeofday(&end,NULL);

    for ( i = 0; i < iters; i++) /* have to do a wait for each child */

    wait(&status);

    forktime = (end.tv_sec – start.tv_sec)*1000000 +
    (end.tv_usec – start.tv_usec);
    avgtime = (double)forktime/(double)iters;

    printf(“Avg fork time =%f microsec\n”, avgtime);

    }
    null_proc()
    {
    }

    https://cs.gmu.edu/~jchen/cs471/programming/fork.c

    thr_shared.c
    #include

    #include

    #include

    #include

    #include

    void *proc();

    int shared_number;

    main()
    {
    int i;

    pthread_t new_thread;

    int sleep_time;

    int seed;

    shared_number = 1;

    printf(“Enter a positive integer for seed: “);

    scanf(“%d”,&seed);

    srand48(seed); /* initialize seed of random number stream */

    /* thr_create(NULL,0,proc,NULL,0,&new_thread);*/

    pthread_create(&new_thread,NULL,proc,NULL);

    /* create new thread */

    for (i = 0; i < 50; i++)

    {

    printf(“MAIN THREAD: i = %d, shared_number = %d\n”,i,shared_number);

    sleep_time = 100000.0*drand48(); /* generate random sleep time */

    printf(“sleep time = %d microseconds\n”,sleep_time);

    usleep(sleep_time);

    shared_number = shared_number + 2;

    }

    pthread_join(new_thread,NULL);

    printf(“MAIN THREAD: DONE\n”);

    }

    https://cs.gmu.edu/~jchen/cs471/programming/thr_shared.c

    void *proc()

    {

    int i = 0;

    int DONE;

    DONE = 0;

    while (!DONE)

    {

    i++;

    if (i%10000 == 0)

    printf(“CHILD THREAD: i = %d,shared_number = %d\n”,i,shared_number);

    if (i > 5000000)

    DONE = 1;

    }

    printf(“CHILD THREAD: DONE\n”);

    }

    proc_shared.c

    #include
    #include
    #include #include
    #include
    #include

    void *proc();

    int shared_number;

    main()
    {
    int i;
    pthread_t new_thread;
    int sleep_time;
    int pid;
    int status;
    int seed;

    shared_number = 1;

    https://cs.gmu.edu/~jchen/cs471/programming/proc_shared.c

    printf(“Enter a positive integer for seed: “);
    scanf(“%d”,&seed);

    srand48(seed); /* initialize random number stream */

    if ((pid = fork()) == 0)

    { /* child process */

    proc();

    exit(0);

    }

    else if (pid == -1) /* error */

    {

    printf(“error %d\n”,errno);

    exit(-1);

    }

    else

    { /* parent process */

    for (i = 0; i < 50; i++)

    {

    printf(“MAIN PROCESS: i = %d, shared_number = %d\n”,i,shared_number);

    sleep_time = 100000.0*drand48(); /* generate random sleep time */

    printf(“sleep time = %d microseconds\n”,sleep_time);
    usleep(sleep_time);
    shared_number = shared_number + 2;
    }

    wait(&status); /* wait for child process */

    printf(“MAIN PROCESS: DONE\n”);

    }
    }

    void *proc()
    {
    int i;
    int DONE;

    DONE = 0;

    i = 0;

    while (!DONE)
    {

    i++;

    if (i%10000 == 0)

    printf(“CHILD PROCESS: i = %d,shared_number = %d\n”,i,shared_number);

    if (i > 5000000)
    DONE = 1;
    }

    printf(“CHILD PROCESS: DONE\n”);

    }

    shared_data.c

    #include
    #include
    #include #include
    #include

    void *proc();

    int shared_number;

    main()
    {
    int i;
    pthread_t new_thread;
    int sleep_time;

    int number;

    int seed;

    shared_number = 1;
    printf(“Enter a positive integer for seed: “);
    scanf(“%d”,&seed);

    srand48(seed);

    pthread_create(&new_thread,NULL,proc,NULL);

    for (i = 0; i < 20; i++)

    {

    number = shared_number;

    printf(“MAIN THREAD: i = %d, shared_number = %d\n”,i,shared_number);
    sleep_time = 100000.0*drand48(); /* generate random sleep time */

    printf(“sleep time = %d microseconds\n”,sleep_time);

    https://cs.gmu.edu/~jchen/cs471/programming/shared_data.c

    usleep(sleep_time);

    shared_number = number + 2;

    }

    pthread_join(new_thread,NULL);

    printf(“MAIN THREAD: DONE\n”);
    }

    void *proc()
    {

    int number,i;

    int sleep_time;

    printf(“CHILD\n”);

    for (i = 0; i < 10; i++)

    {
    number = shared_number;

    printf(“CHILD THREAD: i = %d, shared_number = %d\n”,i,shared_number);

    sleep_time = 100000.0*drand48(); /* generate random sleep time */

    printf(“sleep time = %d microseconds\n”,sleep_time);

    usleep(sleep_time);

    shared_number = number + 200000;

    }
    printf(“CHILD THREAD: DONE\n”);

    }

    SharedData.java

    import java.util.Random;

    public class SharedData extends Thread {

    static Random gRandom = new Random();

    static int gSharedNumber = 0;

    private int gID;

    public SharedData(int id) {

    https://cs.gmu.edu/~jchen/cs471/programming/SharedData.java

    this.gID = id;

    }

    public void run() {

    for(int i=0;i<20;i++) {

    int number = gSharedNumber;

    System.out.println(“SharedData[“+gID+”](i=”+i+”) = “+gSharedNumber);

    try { Thread.sleep(gRandom.nextInt(1000));

    } catch (InterruptedException e) { /* ignore */ }

    if(gID == 0) gSharedNumber = number + 1;

    else gSharedNumber = number + 1000;

    }

    }

    public static void main(String args[]) throws Exception {

    SharedData sd1 = new SharedData(0);

    SharedData sd2 = new SharedData(1);

    sd1.start(); sd2.start();

    sd1.join(); sd2.join();

    }

    }

    What Will You Get?

    We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.

    Premium Quality

    Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.

    Experienced Writers

    Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.

    On-Time Delivery

    Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.

    24/7 Customer Support

    Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.

    Complete Confidentiality

    Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.

    Authentic Sources

    We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.

    Moneyback Guarantee

    Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.

    Order Tracking

    You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.

    image

    Areas of Expertise

    Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.

    Areas of Expertise

    Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.

    image

    Trusted Partner of 9650+ Students for Writing

    From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.

    Preferred Writer

    Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.

    Grammar Check Report

    Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.

    One Page Summary

    You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.

    Plagiarism Report

    You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.

    Free Features $66FREE

    • Most Qualified Writer $10FREE
    • Plagiarism Scan Report $10FREE
    • Unlimited Revisions $08FREE
    • Paper Formatting $05FREE
    • Cover Page $05FREE
    • Referencing & Bibliography $10FREE
    • Dedicated User Area $08FREE
    • 24/7 Order Tracking $05FREE
    • Periodic Email Alerts $05FREE
    image

    Our Services

    Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.

    • On-time Delivery
    • 24/7 Order Tracking
    • Access to Authentic Sources
    Academic Writing

    We create perfect papers according to the guidelines.

    Professional Editing

    We seamlessly edit out errors from your papers.

    Thorough Proofreading

    We thoroughly read your final draft to identify errors.

    image

    Delegate Your Challenging Writing Tasks to Experienced Professionals

    Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!

    Check Out Our Sample Work

    Dedication. Quality. Commitment. Punctuality

    Categories
    All samples
    Essay (any type)
    Essay (any type)
    The Value of a Nursing Degree
    Undergrad. (yrs 3-4)
    Nursing
    2
    View this sample

    It May Not Be Much, but It’s Honest Work!

    Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.

    0+

    Happy Clients

    0+

    Words Written This Week

    0+

    Ongoing Orders

    0%

    Customer Satisfaction Rate
    image

    Process as Fine as Brewed Coffee

    We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.

    See How We Helped 9000+ Students Achieve Success

    image

    We Analyze Your Problem and Offer Customized Writing

    We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.

    • Clear elicitation of your requirements.
    • Customized writing as per your needs.

    We Mirror Your Guidelines to Deliver Quality Services

    We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.

    • Proactive analysis of your writing.
    • Active communication to understand requirements.
    image
    image

    We Handle Your Writing Tasks to Ensure Excellent Grades

    We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.

    • Thorough research and analysis for every order.
    • Deliverance of reliable writing service to improve your grades.
    Place an Order Start Chat Now
    image

    Order your essay today and save 30% with the discount code Happy