Showing posts with label Shared Memory. Show all posts
Showing posts with label Shared Memory. Show all posts

Shared Memory - String - Systems Lab - C Program


Program:

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/wait.h>
#include<sys/types.h>
#include"string.h"
#define shmsize 100
#define shmmode (SHM_R|SHM_W|IPC_CREAT|IPC_EXCL)
#define shmkey (key_t)31
int size;
int main()
{
    int shmid1, shmid2, pid, status;
   
    int *shmdata1, *shmdata2, *shmdata;
   
    char str1[20],str2[20],str3[20];
    int i,j,k,size2,size3;
    struct shmid_ds *shmidds;
    shmid1 = shmget(shmkey,shmsize,shmmode);
    shmdata1 = (int*)shmat(shmid1,0,0);
    shmdata = shmdata1;
    printf("Enter the string:");
    scanf("%s",str1);
    size=strlen(str1);
    for(j=0;j<size;j++)
    {
        *shmdata1=str1[j];
        shmdata1=shmdata1+sizeof(char);
    }
    pid = fork();           
    if(pid == 0)
    {
        printf("Enter the string:");
        scanf("%s",str2);
        size2=strlen(str2);
        for(i=0;i<size2;i++)
        {
            *shmdata1=str2[i];
            shmdata1+=sizeof(char);
        }
        size=size+size2;
    }
    while((pid = wait(&status))!= -1);
    shmdata1=shmdata;
    for(i=0;i<size;i++)
    {
        str3[i]=*shmdata1;
        shmdata1+=sizeof(char);
    }
    str3[size]='\0';
    printf("%s\n\n\n\n\n\n",str3);
shmdt((void*)shmdata);
shmdt((void*)shmdata1);
shmctl(shmid1,IPC_RMID,shmidds);
    return 1;
}

Output:
nn@ubuntu:~$ gcc shmstr.c
nn@ubuntu:~$ ./a.out
Enter the string:hai
Enter the string:helo
haihelo





hai





nn@ubuntu:~$

Matrix Multiplication using Shared Memory- Systems Lab - C Program



//Matrix Multiplication using Shared Memory-OS Lab

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/wait.h>
#include<sys/types.h>
#define shmsize 100
#define shmmode (SHM_R|SHM_W)
#define shmkey (key_t)31

int main() {
    int shmid1, shmid2, pid, status;
    int *shmdata1, *shmdata2, *shmdata;
    int mtx1[10][10], mtx2[10][10];
    int i,j,k,r1,r2,c1,c2;
    struct shmd_ds *shmidds;
    shmid1 = shmget(shmkey,shmsize,shmmode|IPC_CREAT|IPC_EXCL);
    shmdata1 = (int*)shmat(shmid1,0,0);
    shmdata = shmdata1;
    printf("\nShmID: %d ShmData: %d \n",shmid1,*shmdata1);
    printf("Enter the rows and columns of matrix 1:");
    scanf("%d%d",&r1,&c1);
    printf("Enter the matrix 1: \n");
    for(i=0;i<r1;++i)
        for(j=0;j<c1;++j)
            scanf("%d",&mtx1[i][j]);
    printf("\nEnter the rows and columns of matrix 2:");
    scanf("%d%d",&r2,&c2);
    printf("Enter the matrix 2: \n");   
    for(i=0;i<r2;++i)
        for(j=0;j<c2;++j)
            scanf("%d",&mtx2[i][j]);   
    printf("\n Hello,note this...");
    if(r2!=c1) {
        printf("\nCannot Multiply");
        return 0;
    }

    for(i=0;i<r1/2;i++)
        for(j=0;j<c1;j++) {
        *shmdata1 = 0;
        for(k=0;k<c1;k++)
            *shmdata1 += mtx1[i][k]*mtx2[k][j];
            shmdata1 += sizeof(int);
        }
    pid = fork();
    if(pid == 0) {
        for(i=r1/2;i<r1;i++)
            for(j=0;j<c2;j++) {
                *shmdata1 = 0;
                for(k=0;k<c1;k++)
                    *shmdata1 += mtx1[i][k]*mtx2[k][j];
                    shmdata1 += sizeof(int);
            }       
    }
    while((pid = wait(&status))!= -1);
    shmdata1 = shmdata;
    printf("\n\n\nResult from %d\n", getpid());
    for(i=0;i<r1;++i) {
        printf("\n    ");
        for(j=0;j<c2;j++,shmdata1+=sizeof(int))
            printf("%d ",*shmdata1);
    }
    shmdt((void*)shmdata1);
    shmdt((void*)shmdata2);
    shmctl(shmid1,IPC_RMID,shmidds);
    //shmctl(shmid2,IPC_RMID,shmidds);
    return 1;
}


Related Posts Plugin for WordPress, Blogger...