Skip to content

Instantly share code, notes, and snippets.

@navin-mohan
Created April 11, 2018 15:39
Show Gist options
  • Save navin-mohan/9927860f1011c4a349e08ed22706a776 to your computer and use it in GitHub Desktop.
Save navin-mohan/9927860f1011c4a349e08ed22706a776 to your computer and use it in GitHub Desktop.
concat and remove
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
// gcc -pthread concat.c -o concat
// set pshared for interprocess operation
pthread_mutexattr_t attr;
int shmid_mutex,shmid_str;
key_t key;
// shared memory segment for the mutex
pthread_mutex_t *shm_mutex;
// shared memory for the string
char* shm_str;
int main(){
pthread_mutexattr_setpshared(&attr,PTHREAD_PROCESS_SHARED);
key = 1234;
// create shm for mutex
if ((shmid_mutex = shmget(key,sizeof(pthread_mutex_t),IPC_CREAT | 0666)) < 0){
perror("Could not create shm for mutex");
exit(1);
}
if ((shm_mutex = shmat(shmid_mutex,NULL,0)) < 0){
perror("Could not attach shm for mutex");
exit(1);
}
// initialize the mutex lock to the shared memory segment
pthread_mutex_init(shm_mutex,&attr);
key = 4567;
// create shared memory segment for storing the string
if ((shmid_str = shmget(key,sizeof(char)*100,IPC_CREAT | 0666)) < 0){
perror("Could not create shm for string");
exit(1);
}
if ((shm_str = shmat(shmid_str,NULL,0)) < 0){
perror("Could not attach shm for string");
exit(1);
}
// initialize the string in shared memory segment
strcpy(shm_str,"Model Engineering College");
while(1){
// lock before modifying the string
pthread_mutex_lock(shm_mutex);
printf("%s\n",shm_str);
strcpy(shm_str,"Model Engineering College");
// release the lock after modification
pthread_mutex_unlock(shm_mutex);
// sleep for 5 secs
sleep(5);
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>
#include <stdlib.h>
int shmid_mutex,shmid_str;
key_t key;
// shared memory segment for the mutex
pthread_mutex_t *shm_mutex;
// shared memory for the string
char* shm_str;
void remove_e(char* str);
int main()
{
key = 1234;
// get the shared memory segment with the mutex lock
if ((shmid_mutex = shmget(key,sizeof(pthread_mutex_t),IPC_CREAT | 0666)) < 0){
perror("Could not create shm for mutex");
exit(1);
}
if ((shm_mutex = shmat(shmid_mutex,NULL,0)) < 0){
perror("Could not attach shm for mutex");
exit(1);
}
key = 4567;
// get the shared memory segment with string
if ((shmid_str = shmget(key,sizeof(char)*100,IPC_CREAT | 0666)) < 0){
perror("Could not create shm for string");
exit(1);
}
if ((shm_str = shmat(shmid_str,NULL,0)) < 0){
perror("Could not attach shm for string");
exit(1);
}
while(1){
// lock before modifying the string
pthread_mutex_lock(shm_mutex);
remove_e(shm_str);
// release the lock after modification
pthread_mutex_unlock(shm_mutex);
// sleep for 7 secs
sleep(7);
}
return 0;
}
void remove_e(char* str){
int i=0;
for(i=0;str[i] != '\0';++i){
if(str[i] == 'e' || str[i] == 'E'){
str[i] = '#';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment