Example #1
0
int main(int argc, char *argv[])
{
    char write_buf[BUF_MAX];
    char read_buf[BUF_MAX];

    memset(write_buf, 0, sizeof(write_buf));
    memset(read_buf, 0, sizeof(read_buf));

    if (share_mem_init(0x36f9) < 0) {
        printf("err init.\n");
        return -1;
    }

    int i = 0;
    for (i = 0; i < BUF_MAX; i++) {
        write_buf[i] = i;
    }

    share_mem_write(100, write_buf, BUF_MAX);
    share_mem_read(100, read_buf, BUF_MAX);

    for (i = 0; i < BUF_MAX; i++) {
        printf("%4x ", read_buf[i]);
        if (i % 10 == 0) {
            printf("\n");
        }
    }
    printf("\n");

    return 0;
}
Example #2
0
int main()
{

	int fd[2];
	pid_t pid;

	char buff[255];
	int len;

	//create pipe
	pipe(fd);
	
	//create a child
	pid = fork();

	//child forking
	if (-1 == pid)
	{ 
		// indicates the fork was unsuccessful
		perror("cannot fork\n");
		exit(1);
	}	
	else if (0 == pid)
	{
		//child
		
		//close unwanted end
		close(fd[1]);
				
		//read the data from fd
		read(fd[0], &len, sizeof(len));
		read(fd[0], buff, len);	
		printf("\nString: %s\n", buff);
		
		//reverse string
		reverse_str(buff, strlen(buff));
		printf("Reversed String: %s\n", buff);
	
		//write reversed string to shared memory
		share_mem_write(buff);	
	}
	else
	{
		//parent
		
		//enter the string that you want to save
		printf("Enter the string: ");
		fgets(buff, 255, stdin);

		//strcpy(buff, "Hello");		
		len = strlen(buff);
		len++; //to account for null

		//close unwanted fd
		close(fd[0]);

		//write the data into this fd
		write(fd[1], &len, sizeof(len));
		write(fd[1], buff, len);
		
		sleep(5);

		//read from shared memory
		share_mem_read(buff);
		printf("String: %s\n", buff);
	}	
}