I'm trying to write a function that has mmap within it, however when I try to access the memory from main(), it gets a segfault. Does anyone have any idea why?
Please ignore the MPI headers - it's for the later part of the project. I have commented out the mprotect line to see that it is an mmap fault as opposed to the handler not necessarily working
net_map.cpp:
#include <iostream>
#include <signal.h>
#include <sys/mman.h>
#include <mpi.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "net_map.h"
using namespace std;
void handler(int sig, siginfo_t* info, void* other)
{
void* address = info->si_addr;
cout << "\nSegfault Detected! [Address: " << address << "]\n" << endl;
int unprotectresult = mprotect(address, SIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
if (unprotectresult == 0)
{
cout << "\nSuccessful mprotect (memory unprotected)!\n" << endl;
}else
{
cout << "\nMprotect unsuccessful\n" << endl;
}
}
void netmap (char filename[], char* mapped)
{
int file;
file = open(filename, O_RDWR);
mapped = (char*) mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, file, 0);
if (mapped == MAP_FAILED)
{
cout << "\nMap Failed!" << endl;
}
cout << mapped << endl;
}
main.cpp
#include <iostream>
#include <signal.h>
#include <sys/mman.h>
#include <mpi.h>
#include <unistd.h>
#include <array>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <array>
#include "net_map.h"
using namespace std;
int main(int argc, char* argv[])
{
struct sigaction segaction;
memset(&segaction, 0, sizeof(segaction));
segaction.sa_flags = SA_SIGINFO;
sigemptyset(&segaction.sa_mask);
segaction.sa_sigaction = handler;
sigaction(SIGSEGV, &segaction, NULL);
int i;
char* mapped;
networkpage sheet;
char filename[] = "hello.txt";
netmap(filename, mapped);
sheet.address = (void*)mapped;
cout << "\nAddress: " << sheet.address << endl;
//mprotect(sheet.address, SIZE, PROT_NONE);
memcpy(sheet.address, "k", 1);
munmap(sheet.address, SIZE);
return 0;
}
Related
I am trying to compile the following code in visual studio 2019:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string>
int main()
{
char buffer[5];
char* p;
buffer[0] = 3;
p = _cgets(buffer); // problem with _cgets
printf("\ncgets read %d symbols: \"%s\"\n", buffer[1], p);
printf("A pointer is returned %p, buffer[2] on %p\n", p, &buffer);
return 0;
}
But I see the following error:
main.cpp(11,9): error C3861: '_cgets': identifier not found
_cgets is no longer available, use _cgets_s instead:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string>
int main()
{
char buffer[5];
size_t sizeRead;
auto error = _cgets_s(buffer, &sizeRead);
printf("\ncgets read %zu symbols: \"%s\"\n", sizeRead, buffer);
return 0;
}
Or the much simpler c++ code:
#include <iostream>
#include <string>
int main()
{
std::string buffer;
std::getline(std::cin, buffer);
std::cout << "read " << buffer.size() << " characters \"" << buffer << "\"\n";
}
My question is how to correctly write a protobuf message in text format to a file. In the program below I create a NetParameter object which is a message
field in message field in caffe.proto.
When I call print() and PrintToString() they return failure.
I can print the mesaage on the console with printf("%s", data.c_str()); but notting is written to file?
#include <fcntl.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <stdint.h>
#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <vector>
#include <string>
#include "caffe.pb.h"
#include <iostream>
#include <caffe/caffe.hpp>
using namespace std;
using google::protobuf::Message;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
using google::protobuf::TextFormat;
int main()
{
caffe::NetParameter param;
const char *filename = "./test.prototxt";
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
cout << "File not found: " << filename;
google::protobuf::io::FileOutputStream* output = new google::protobuf::io::FileOutputStream(fd);
param.set_name("AlexNet");
param.add_input_dim(0);
param.add_input_dim(1);
param.add_input_dim(2);
param.set_input_dim(0, 3);
param.set_input_dim(1, 3);
param.set_input_dim(2, 3);
std::string data;
bool success = google::protobuf::TextFormat::PrintToString(param, &data);
std::cout << "bool is " << success << std::endl;
printf("%s", data.c_str());
bool success1 = google::protobuf::TextFormat::Print(param, output);
std::cout << "bool is " << success << std::endl;
close(fd);
return 0;
}
Does anybody know how to redirect stderr into a file without buffering in? if it is possible could you show me a simple code in c++ language for linux (Centos 6) operating system..?!
In C
#include <stdio.h>
int
main(int argc, char* argv[]) {
freopen("file.txt", "w", stderr);
fprintf(stderr, "output to file\n");
return 0;
}
In C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int
main(int argc, char* argv[]) {
ofstream ofs("file.txt");
streambuf* oldrdbuf = cerr.rdbuf(ofs.rdbuf());
cerr << "output to file" << endl;
cerr.rdbuf(oldrdbuf);
return 0;
}
Another way to do this is with the following dup2() call
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <unistd.h>
using std::cerr;
using std::endl;
int main() {
auto file_ptr = fopen("out.txt", "w");
if (!file_ptr) {
throw std::runtime_error{"Unable to open file"};
}
dup2(fileno(file_ptr), fileno(stderr));
cerr << "Write to stderr" << endl;
fclose(file_ptr);
}
I have a server and a client program. Server program prompts user to enter text, then sends user input to client. The client prints out user text to screen. So far server program prompts user to enter text and when I run client program it displays nothing. Any recommendations to make programs work. Below are both programs.
// server.cpp
// g++ -o server server.cpp -lpthread -lrt
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <iostream>
using namespace std;
string UserInput(string);
#define SHMSZ 27
char SEM_NAME[]= "vik";
int main(void)
{
char ch;
int shmid;
key_t key;
char *shm,*s;
sem_t *mutex;
string input;
//name the shared memory segment
key = 1000;
//create & initialize semaphore
mutex = sem_open(SEM_NAME,O_CREAT,0644,1);
if(mutex == SEM_FAILED)
{
perror("unable to create semaphore");
sem_unlink(SEM_NAME);
exit(-1);
}
//create the shared memory segment with this key
shmid = shmget(key,SHMSZ,IPC_CREAT|0666);
if(shmid<0)
{
perror("failure in shmget");
exit(-1);
}
//attach this segment to virtual memory
shm = (char*) shmat(shmid,NULL,0);
//start writing into memory
s = shm;
// Enter user input
//cout << "Enter your input: ";
//cin >> input;
// Display user input
//cout << "You entered: "<< input << endl;
//return 0;
while(1)
{
cout << "Enter your input: ";
cin >> input;
sem_wait(mutex);
//*s++ = count;
sem_post(mutex);
//return 0;
}
//the below loop could be replaced by binary semaphore
while(*shm != '*')
{
sleep(1);
}
sem_close(mutex);
sem_unlink(SEM_NAME);
shmctl(shmid, IPC_RMID, 0);
_exit(0);
}
-
// client.cpp
// g++ -o client client.cpp -lpthread -lrt
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
string UserInput(string);
#define SHMSZ 27
char SEM_NAME[]= "vik";
int main(void)
{
char ch;
int shmid;
key_t key;
char *shm,*s;
sem_t *mutex;
string input;
//name the shared memory segment
key = 1000;
//create & initialize existing semaphore
mutex = sem_open(SEM_NAME,0,0644,0);
if(mutex == SEM_FAILED)
{
perror("reader:unable to execute semaphore");
sem_close(mutex);
exit(-1);
}
//create the shared memory segment with this key
shmid = shmget(key,SHMSZ,0666);
if(shmid<0)
{
perror("reader:failure in shmget");
exit(-1);
}
//attach this segment to virtual memory
shm = (char*) shmat(shmid,NULL,0);
//start reading
s = shm;
while(1)
{
cout << "You entered: " << input << endl;
//cin >> input;
sem_wait(mutex);
putchar(*s);
sem_post(mutex);
return 0;
}
//once done signal exiting of reader:This can be replaced by another semaphore
*shm = '*';
sem_close(mutex);
shmctl(shmid, IPC_RMID, 0);
exit(0);
}
I'm creating a simple chess game which uses shared memory. There are two players (Black,White) in the game and they are working in Strict Alternation principle. I get input from every player in turn-based sense and use them. My question is: I want the user to give the input like mov(a1,b2). After getting the input First: I want to validate if it is in the correct format and then I want to use the a1,b2 values in a function. Here is the part of my code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define SHMSZ 27
int main() {
int shmid;
key_t key;
char *shm;
string cmd;
int check =1;
key = 111;
/*
* Locate the segment.
*/
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
return 0;
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = (char*) shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
return 0;
}
cout << *shm << endl;
while(check)
{
if(*shm == '*') //checks if other player finished the game
{
cout<<"End of the game. Thank you for playing." <<endl;
*shm ='*';
exit(1);
}
while(*shm == 'B')
{
cout << "Enter a move" << endl;
cin >> cmd;
if(cmd=="eog") // Finish the game
{
cout<<"End of the game. Thank you for playing." <<endl;
*shm ='*';
exit(1);
}
*shm = 'W';
}
}
return 0;
}