Heading hidden

C Programming Assignment Help

C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.

Audience

This tutorial is designed for software programmers with a need to understand the C programming language starting from scratch. This tutorial will give you enough understanding on C programming language from where you can take yourself to higher level of expertise.

Prerequisites

Before proceeding with this tutorial, you should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages will help you in understanding the C programming concepts and move fast on the learning track.

Execute C Online

For most of the examples given in this tutorial you will find Try it option, so just make use of this option to execute your C programs at the spot and enjoy your learning.

Try following example using Try it option available at the top right corner of the below sample code box

Best C Programming Homework Help

Programming for many turns out to be a perfect source of excitement and fun. However, not all feel the same way. Some put in a huge amount of efforts in understanding the basics of programming, but still are unable to get things right. Things turn worse when they are not able to complete their C program assignments on time. This greatly affects academics. Students try hard towards finding someone who can offer the most appropriate C assignment help, but none of it seems to be working. This is when we come to your rescue. We can offer you the best quality C programming assignments that can help you in excelling in your academics.

Why students require C programming homework help?

  • students fail to get the right guidance at school;
  • not all are capable of understanding the basic concepts of C programming easily;
  • they simultaneously need to deal with various other academic activities;

Finding the right C online help is definitely not easy, as not all online sources of information are authentic. Moreover, due to the lack of appropriate guidance from their teachers, students usually find it difficult to work on their C programming assignments. We exactly understand what the students these days are going through and therefore, make great efforts towards offering you the best possible C assignment help at the most reasonable price. All you need to do is just give us your topic and we will start working upon it right from scratch.

Why should you avail our C help service?

  • we offer 24/7 customer support;
  • our writers are capable of working on all C programming assignment topics;
  • all the sources of information we rely on are completely authentic;

In case you hold any query, you can get in touch with us at any point of time. We have a team of well-qualified and highly experienced writers and therefore what you will be receiving back is sure to be of the best quality. Unlike various other c programming online firms, we in no way entertain plagiarism and so the content comes from a very reliable source and is therefore, highly authentic and original. You can avail our C online help at the time of your convenience, as our customer support team operates 24 hours a day 7 days a week.

Only Free Assignment Help offer you the most appropriate C programming homework help:

  • we complete your C programming assignment well before the set deadline;
  • you have the liberty to choose your own writer;
  • you are free to make your own suggestions;

We truly understand how important it is for the students to submit their C program assignments on time and therefore, our writers make great efforts towards completing all your projects well before the set time limit, so that you are able to make your submission right on time. Moreover, in case you have any suggestions, just discuss them with your expert and he will accordingly do your c programming online. Without a doubt, we are capable of offering you almost all types of help with C homework.

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define NUM_THREADS 4
#define BUFFER_SIZE 10

pthread_mutex_t mutex;
sem_t full;
sem_t empty;
int cookieCount;

// producer calls this function to produce a cookie
void insert_item(int pid) {
  cookieCount ++;
  printf("producer #%d inserted a cookie. Total: %d\n", pid, cookieCount);
  fflush(stdout);
}

// consumer calls this function to remove a cookie
void remove_item(int pid) {
  cookieCount --;
  printf("consumer #%d removed a cookie. Total: %d\n", pid, cookieCount);
  fflush(stdout);
}

void *producer_procedure(void *p) {
  int pid = (int)(unsigned long)p;
  int times = 5; 
  int i = 0;
  for (i = 0; i < times; i++) {
    if (sem_wait(&empty) != 0) { // wait until there is free space
      fprintf(stderr, "Could not wait on the semaphore.\n");
      break;
    }

    if (pthread_mutex_lock(&mutex) != 0) {
      fprintf(stderr, "Could not lock the mutex.\n"); // should not happen
      break;
    }

    insert_item(pid); // insert item

    pthread_mutex_unlock(&mutex);

    if (sem_post(&full) != 0) { // increase the full slot by 1
      fprintf(stderr, "Could not post on the semaphore.\n");
    }

    sleep(1);
  }
  return NULL;
}

void *consumer_procedure(void *p) {
  int pid = (int)(unsigned long)p;
  int times = 5; 
  int i = 0;
  for (i = 0; i < times; i++) {
    if (sem_wait(&full) != 0) { // wait until there is at least one cookie
      fprintf(stderr, "Could not wait on the semaphore.\n");
      break;
    }

    if (pthread_mutex_lock(&mutex) != 0) {
      fprintf(stderr, "Could not lock the mutex.\n"); // should not happen
      break;
    }

    remove_item(pid); // remove item

    pthread_mutex_unlock(&mutex);

    if (sem_post(&empty) != 0) { // increase the empty slot by 1
      fprintf(stderr, "Could not post on the semaphore.\n");
    }

    sleep(1);
  }

  return NULL;
}

int main(int argc, char *argv[]) {
  pthread_t producers[NUM_THREADS];
  pthread_t consumers[NUM_THREADS];
  int i;

  // initilize the mutex
  if (pthread_mutex_init(&mutex, NULL) != 0) {
    fprintf(stderr, "Could not initialize the mutex.\n");
    return 0;
  }

  // initialize the semphore
  if (sem_init(&empty, 0, BUFFER_SIZE) != 0) {
    fprintf(stderr, "Could not initialize the semaphore.\n");
    return 0;
  }
  if (sem_init(&full, 0, 0) != 0) {
    fprintf(stderr, "Could not initialize the semaphore.\n");
    return 0;
  }

  // create the consumers and producers
  for (i = 0; i < NUM_THREADS; i++) {
    producers[i] = 0;
    consumers[i] = 0;
  }

  for (i = 0; i < NUM_THREADS; i++) {
    // parse the thread id (i) as the parameter to the procedure
    if (pthread_create(&producers[i], NULL, producer_procedure, (void *)(unsigned long)i) != 0) {
      fprintf(stderr, "Could not create producer thread\n");
      break;
    }
  if (pthread_create(&consumers[i], NULL, consumer_procedure, (void *)(unsigned long)i) != 0) {
      fprintf(stderr, "Could not create consumer thread\n");
      break;
    }
  }

  // wait until all the producers and consumers terminate
  for (i = 0; i < NUM_THREADS; i++) {
    if (producers[i] > 0) {
      pthread_join(producers[i], NULL);
    }
    if (consumers[i] > 0) {
      pthread_join(consumers[i], NULL);
    }
  }

  // destroy the semaphores and mutex before the system terminates
  sem_destroy(&empty);
  sem_destroy(&full);
  pthread_mutex_destroy(&mutex);

  return 0;
}