Help me see what is wrong with the Program (Memory Sharing)

Source: Internet
Author: User
For more information, see what is wrong with the Program (Memory Sharing)-Linux general technology-Linux programming and kernel information. /* Memory1.c */

# Include
# Include
# Include
# Include

# Define SHMKEY 75
Int shmid;

Int main ()
{
Int * pint = 0, m, n, result;
Char * addr1 = 0, operator;
Int judge;


// Printf ("m = % d, operator = % c, n = % d \ n", m, operator, n); // for debugging

Shmid = shmget (SHMKEY, 0777 | IPC_CREAT );
If (-1 = shmid)
{
Printf ("creat shm error ");
Return-1;
}
Addr1 = (char *) shmat (shmid, 0, 0 );
If (-1 = (int) addr1)
{
Printf ("attach shm error! \ N ");
Return-1;
}
Printf ("addr1 0x % x \ n", addr1 );
While (1)
{/* Subinput */
Printf ("Enter the formula for calculation: (such as 12 + 45) \ n"); // note that there is no space in the middle
Scanf ("% d % c % d", & m, & operator, & n );
Pint = (int *) addr1;
* Pint = 0;
Pint ++;
* Pint = m;
Pint ++;
* Pint = (int) operator;
Pint ++;
* Pint = n;
Pint ++;
Pint = (int *) addr1;
* Pint = 256;
While (* pint! = 255 );
Pint ++;
Result = * pint;
Pint --;
* Pint = 0;
Printf ("reslt = % d \ n", result );
Printf ("zookeeper? Too many requests. (0 continue/1 end) \ n ");
Scanf ("% d", & judge );
If (judge = 1)
{
Break;
}
}
Return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~

/* Memory2.c */

# Include
# Include
# Include
# Include

Int shmid;

Int main ()
{
Int * pint, result, m, n;
Char * addr2;
Char operator;

Shmid = shmget (75,1024, 0777 | IPC_CREAT );
If (-1 = shmid)
{
Printf ("creat shm error ");
Return-1;
}
Addr2 = shmat (shmid, 0, 0 );
If (-1 = (int) addr2)
{
Printf ("attach shm error! \ N ");
Return-1;
}

Pint = (int *) addr2;
While (0 = * pint );
Pint ++;
M = * pint;
Pint ++;
Operator = * pint;
Pint ++;
N = * pint;
Switch (operator)
{
Case '+': result = m + n; break;
Case '-': result = m-n; break;
Case '*': result = m * n; break;
Case '/': result = m/n; break;
}
Pint = (int *) addr2;
* Pint = 255;
Pint ++;
* Pint = result;
Return 0;

}
~~~~~~~~~~~~~~~~~~

Run:

# Gcc-o memory1 memory1.c

# Gcc-o memory2 memory22.c

#./Memory1 // window 1

#./Memory2 // window 2

Window 1 appears // when the result ends, select whether to continue as prompted.

#./Memory2 // continue testing

Function to be implemented: file memory1 writes an operation formula to the shared memory segment, and then file memory2 writes the calculation result to the memory again. Then memory1 reads the result from the memory and outputs the result.
Problem: All the results below four digits are correct, but the results with five or more digits are sometimes right and sometimes wrong .. I don't know why. Please help me find out where the problem is...
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.