Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // TestVlad
- //
- // Created by Efraim Budusan on 30/05/16.
- // Copyright © 2016 Efraim Budusan. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- char text[500];
- int charToRemove = 0;
- int n;
- pthread_mutex_t eliminate= PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t mutexes[100];
- void * popThread( void * arg)
- {
- int i = (int)(size_t)arg;
- while (1) {
- pthread_mutex_lock(&eliminate);
- memmove(text, text+1, strlen(text));
- pthread_mutex_unlock(&eliminate);
- printf("%s \n",text);
- pthread_mutex_lock(&mutexes[i]);
- }
- return NULL;
- }
- void * generatorThread()
- {
- while(1)
- {
- charToRemove = rand() % strlen(text);
- for (int i=0;i<n;i++) {
- pthread_mutex_unlock(&mutexes[i]);
- }
- sleep(2);
- }
- return NULL;
- }
- int main(int argc, const char * argv[]) {
- // insert code here...
- srand(time(0));
- printf("Enter the text:");
- fgets(text, sizeof(text), stdin);
- printf("Enter the no of threads:");
- scanf("%d",&n);
- pthread_t th[n];
- pthread_t gThread;
- pthread_create(&gThread, NULL, &generatorThread , NULL);
- for (int i=1;i<=n;i++)
- {
- pthread_mutex_init(&mutexes[i],NULL);
- pthread_mutex_unlock(&mutexes[i]);
- pthread_create(&th[i], NULL, &popThread, (void*)(size_t)i );
- }
- pthread_join(gThread , NULL);
- for (int i=1;i<=n;i++)
- {
- pthread_join(th[i] , NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement