Unix - No puedo leer memoria compartida

 
Vista:

No puedo leer memoria compartida

Publicado por backgor (2 intervenciones) el 07/12/2018 04:52:57
como compilo en opn sco o que libreria uso?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
 
#define SHMSZ     27
#define MAX_SAMPLES 100
#define MAX_SAMPLES_THETA 50
#define DIST 10
#define PI 3.14159265
        /* ranf() is uniform in 0..1 */
 
float box_muller(float m, float s);     /* normal random variate generator */
 
int main()
{
    char c;
    int shmidd,shmidt;
    key_t keyd,keyt;
    char *shmd, *shmt;
    int i,j;
    float distances[MAX_SAMPLES];
    float angles[MAX_SAMPLES_THETA];
    float anglesD[MAX_SAMPLES];
        float mu,sigma,delta_theta;
 
    struct timespec tim, tim2;
    tim.tv_sec = 1;
    tim.tv_nsec = 0;
 
    keyd = 1234;
    if ((shmidd = shmget(keyd, SHMSZ, IPC_CREAT | 0666)) < 0) {
        perror("shmget");
        return(1);
    }
    if ((shmd = shmat(shmidd, NULL, 0)) == (char *) -1) {
        perror("shmat");
        return(1);
    }
    keyt = 5678;
    if ((shmidt = shmget(keyt, SHMSZ, IPC_CREAT | 0666)) < 0) {
        perror("shmget");
        return(1);
    }
    if ((shmt = shmat(shmidt, NULL, 0)) == (char *) -1) {
        perror("shmat");
        return(1);
    }
 
        mu=0;
        sigma=25;
 
        sleep(3);
 
    for(i=0;i<MAX_SAMPLES_THETA;i++)
        {
                angles[i]=box_muller(mu,sigma);
        }
 
        j=-1;
    for(i=0;i<MAX_SAMPLES_THETA-1;i++)
        {
                if (j++<MAX_SAMPLES) anglesD[j]=angles[i];
        if (j++<MAX_SAMPLES){
                        delta_theta=abs(angles[i+1]-angles[i])/2;
                        if (angles[i+1]>angles[i])
                                anglesD[j]= angles[i]+delta_theta;
                        else
                                anglesD[j]= angles[i]-delta_theta;
                }
        }
 
    for(i=0;i<j;i++){
                distances[i]=DIST/cos(anglesD[i]/180*PI);
        }
 
        for(i=0;i<j;i++){
          if(nanosleep(&tim , &tim2) < 0 ) {
                        printf("Nano sleep failed \n");
                return -1;
                }
           sprintf(shmd,"%f",distances[i]);
           if (i%2==0){
                        sprintf(shmt,"%f",anglesD[i]);
           }else{
                        strcpy(shmt,"--");
           }
        }
return(0);
 
}
 
float box_muller(float m, float s)      /* normal random variate generator */
{                                       /* mean m, standard deviation s */
        float x1, x2, w, y1;
        static float y2;
        static int use_last = 0;
 
        if (use_last)                   /* use value from previous call */
        {
                y1 = y2;
                use_last = 0;
        }
        else
        {
                do {
                        x1 = 2.0 * ((double)(rand())/RAND_MAX)- 1.0;
                        x2 = 2.0 * ((double)(rand())/RAND_MAX) - 1.0;
                        w = x1 * x1 + x2 * x2;
                } while ( w >= 1.0 );
 
                w = sqrt( (-2.0 * log( w ) ) / w );
                y1 = x1 * w;
                y2 = x2 * w;
                use_last = 1;
        }
 
        return( m + y1 * s );
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder