إعـــــــلان

تقليص
لا يوجد إعلان حتى الآن.

Shared Memory Access

تقليص
X
 
  • تصفية - فلترة
  • الوقت
  • عرض
إلغاء تحديد الكل
مشاركات جديدة

  • Shared Memory Access


    client
    كود:
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<string.h>
    #include<sys/ipc.h>
    #include<sys/shm.h>
    #include<sys/stat.h>
    
    
    #define ERROR (void *) -1
    
    void error(const char *string)
    {
    		perror(string);
    		_exit(1);
    }
    
    int main(int argc , char *argv[])
    {
    		char *addr; /*shared memory addre */
    		char replay[] = "Ok I Got It\n";
    
    		int key;
    		int z;
    
    		if(argc < 2)
    		{
    				printf("Usage %s Key\n",argv[0]);
    				_exit(0);
    		}
    
    		key = atoi(argv[1]);
    		printf("[+]Starting\n"
    						"[+]Got Key %d\n",key);
    		addr = shmat(key , NULL , 0);
    		if(addr == ERROR)
    		{
    				error("[!]Shmat()");
    		}
    
    		printf("[+]Memory Attached\n");
    		printf("Address %p has %s\n",addr , addr);
    
    		printf("[+]Writing To Memory\n");
    
    		memcpy(addr , replay , sizeof(replay));
    
    		printf("[+]Detach\n");
    
    		z = shmdt(addr);
    		if(z < 0)
    		{
    				error("[!]Shmdt()");
    		}
    
    		_exit(0);
    }
    server :
    كود:
    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/shm.h>
    #include<sys/ipc.h>
    #include<sys/stat.h>
    #include<sys/wait.h>
    
    #define ERROR (void *) -1
    
    int main(int argc , char *argv[])
    {
    		char *addr;
    		char hello[] = "St0rM-Memories\n";
    
    		struct shmid_ds buffer;
    		int seg_size;
    		int seg_id;
    		int z;
    
    		pid_t chpid;
    		int stat;
    
    		char *args[2];
    
    		if(argc < 2)
    		{
    				printf("Usage %s client\n",argv[0]);
    				_exit(0);
    		}
    
    		args[0] = strdup(argv[1]); /*copy program name*/
    
    		printf("[+]Staring\n");
    
    		seg_id = shmget(IPC_PRIVATE , 0x6400 , IPC_CREAT | IPC_EXCL |
    												S_IRUSR | S_IWUSR );
    		if(seg_id < 0)
    		{
    				perror("[!]ShmGet()");
    				_exit(1);
    		}
    
    		printf("[+]Memory Creating Attaching Now\n");
    
    		args[1] = malloc(sizeof(int));
    		sprintf(args[1] , "%d" ,seg_id);
    
    		addr = shmat(seg_id , NULL , 0);
    
    		if(addr == ERROR )
    		{
    				perror("[!]ShmAt()");
    				_exit(1);
    		}
    
    		printf("[+]Memory Attached\n");
    
    		z = shmctl(seg_id , IPC_STAT , &buffer);
    		if(z < 0)
    		{
    				perror("[!]ShmCtl");
    				_exit(1);
    		}
    		printf("Memory At %p Size = %d\n", addr ,buffer.shm_segsz);
    
    		memcpy(addr , hello , sizeof(hello));
    
    		printf("[+]Running %s with id %s Now\n",args[0], args[1]);
    
    		chpid = fork();
    
    		if(chpid == -1)
    		{
    				perror("[!]Fork()");
    				_exit(1);
    		}else if(chpid == 0)
    		{
    				execvp(argv[1], args);
    
    				perror("[!]execvp()");
    				_exit(1);
    		}
    		else
    		{
    				z = shmctl(seg_id , IPC_STAT , &buffer);
    				if(z < 0)
    				{
    						perror("[!]Shmctl()");
    						_exit(1);
    				}
    				printf("Currently Attached Processes %d\n",buffer.shm_nattch);
    				wait(&stat); /*wait for child*/
    		}
    
    		printf("Memory %p Has %s\n",addr , addr);
    
    		z = shmctl(seg_id , IPC_RMID , NULL);
    		if(z < 0)
    		{
    				perror("[!]Shmctl()");
    				_exit(1);
    		}
    
    		printf("[+]Job Complete\n");
    		return 0;
    }
    2 unrelated processes , communicate trough shared memory

    first step : allocate this memory shmat(2)
    second step : attach this memory to our process shmat(2);
    then do what you wanna do with it ; any modification with this memory will affect other processes ;

    if you noticed that we used execvp function to create another process with another program into it

    compile both of them and run them as :

    ./shm_serv ./shm_client

    and you should get

    [+]Staring
    [+]Memory Creating Attching Now
    [+]Memory Attached
    Memory At 0xb7ef2000 Size = 25600
    [+]Running ./shm_client with id 1277954 Now
    Currently Attached Processes 2
    [+]starting
    [+]Got Key 1277954
    [+]Memory Attached
    Address 0xb7f23000 has St0rM-Memories

    [+]Writing To Memory
    [+]Detach
    Memory 0xb7ef2000 Has Ok I Got It

    [+]Job Complete
    remember : shared memory are global ; that mean any program that has the key of this memory can read it ;
    take a look at :
    كود:
    man ipcs
    man ipcrm
    use it carefully
    BOOOF , I AM GONE
    Still , you gotta wait for my PRESENT :D
    C programming arabic Tutorial|Programming-fr34ks
يعمل...
X
😀
🥰
🤢
😎
😡
👍
👎