Run Shared Memory program on cygwin - windows

I have tried to run the following code on cygwin in windows; there will be no compilation errors in it but when I tried to run it I have Bad system call (core dumped) error.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
void main ( )
{ int shm_id; key_t mem_key;
int *shm_ptr;
mem_key = ftok(".", 'a');
shm_id = shmget(mem_key, sizeof(int), IPC_CREAT | 0666);
if (shm_id < 0)
{ printf("*** shmget error (server) ***\n");
exit(1);
}
shm_ptr = (int *) shmat(shm_id, NULL, 0);
/* attach */
if ((int) shm_ptr == -1)
{
printf("*** shmat error (server) ***\n");
exit(1);
}
}
Why I have this error? Can I solve it?
Thanks a lot.

You need to configure and start cygserver.

Related

Solution to avoid the error: "Entry point not found " even though I'm checking the OS version before callingGetTcpTable2 api on windows XPindows XP?

I'm aware that the GetTcpTable2 api is supported only on windows vista and above versions, hence the code checks for the OS version and only then enters the loop that calls the api. I'm compiling the code on windows 7, visual studio 2008 and the executable runs fine on windows 7 and other OS except Windows XP, the error thrown is :
The code snippet is:`
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
// Need to link with Iphlpapi.lib and Ws2_32.lib
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */
int main()
{
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx((OSVERSIONINFO *)&osvi);
if(osvi.dwMajorVersion>=6)
{
// Declare and initialize variables
PMIB_TCPTABLE2 pTcpTable;
ULONG ulSize = 0;
DWORD dwRetVal = 0;
char szLocalAddr[128];
char szRemoteAddr[128];
struct in_addr IpAddr;
int i;
pTcpTable = (MIB_TCPTABLE2 *) MALLOC(sizeof (MIB_TCPTABLE2));
if (pTcpTable == NULL)
{
printf("Error allocating memory\n");
return 1;
}
ulSize = sizeof (MIB_TCPTABLE);
// Make an initial call to GetTcpTable2 to
// get the necessary size into the ulSize variable
if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) ==
ERROR_INSUFFICIENT_BUFFER)
{
FREE(pTcpTable);
pTcpTable = (MIB_TCPTABLE2 *) MALLOC(ulSize);
if (pTcpTable == NULL)
{
printf("Error allocating memory\n");
return 1;
}
}
}
else
{
printf("Unsupported OS");
}
return 0;
}
`How do I get the executable to work on Windows XP without crashing/throwing the error shown in attached image?

Catch system calls on Mac OS X

I'm trying to catch all systems-calls called by a given PID with a self-made program (I cant use any of strace, dtruss, gdb...). So i used the function
kern_return_t task_set_emulation(task_t target_port, vm_address_t routine_entry_pt, int routine_number) declared in /usr/include/mach/task.h .
I've written a little program to catch the syscall write :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <mach/mach.h>
#include <mach/mach_vm.h>
void do_exit(char *msg)
{
printf("Error::%s\n", msg);
exit(42);
}
int main(void)
{
mach_port_t the_task;
mach_vm_address_t address;
mach_vm_size_t size;
mach_port_t the_thread;
kern_return_t kerr;
//Initialisation
address = 0;
size = 1ul * 1024;
the_task = mach_task_self(); //Get the current program task
kerr = mach_vm_allocate(the_task, &address, size, VM_MEMORY_MALLOC); //Allocate a new address for the test
if (kerr != KERN_SUCCESS)
{ do_exit("vm_allocate"); }
printf("address::%llx, size::%llu\n", address, size); //debug
//Process
kerr = task_set_emulation(the_task, address, SYS_write); //About to catch write syscalls
the_thread = mach_thread_self(); //Verify if a thread is opened (even if it's obvious)
printf("kerr::%d, thread::%d\n", kerr, the_thread); //debug
if (kerr != KERN_SUCCESS)
{ do_exit("set_emulation"); }
//Use some writes for the example
write(1, "Bonjour\n", 8);
write(1, "Bonjour\n", 8);
}
The Output is :
address::0x106abe000, size::1024
kerr::46, thread::1295
Error::set_emulation
The kernel error 46 corresponds to the macro KERN_NOT_SUPPORTED described as an "Empty thread activation (No thread linked to it)" in /usr/include/mach/kern_return.h, and happend even before i'm calling write.
My question is: What did I do wrong in this process? Kern_not_supported does mean that it's not implemented yet, instead of a meaningless thread problem?
The source code in XNU for the task_set_emulation is:
kern_return_t
task_set_emulation(
__unused task_t task,
__unused vm_offset_t routine_entry_pt,
__unused int routine_number)
{
return KERN_NOT_SUPPORTED;
}
Which means task_set_emulation is not supported.

gcc using unlink and readdir, 7 days old files needs to be deleted

Using this code fetched from google.
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
struct dirent *entry;
DIR *dp;
chdir("/mnt/shared");
dp = opendir(".");
while( (entry = readdir(dp)) != NULL ) {
if ( strcmp(entry->d_name, ".") &&strcmp(entry->d_name, "..") ){
unlink(entry->d_name);
}
}
}`
In this could it be possible to delete files older than 7 days from the current date?
In perl i tried as follows, but wondering this could be achived with your help?
my $now = time();
my $DATEAGE = 60*60*24*7;
for my $file (#file_list) {
my #stats = stat($file);
if ($now-$stats[9] > $DATEAGE) {
print "$file\n";}
Build the full string of the file and use several syscalls(2) (notably stat(2)) ; read Advanced Linux Programming
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
struct dirent *entry;
DIR *dp;
time_t weekago;
time(&weekago);
weekago -= 86400*7;
dp = opendir("/mnt/shared");
if (!dp) { perror("/mnt/shared"); exit(EXIT_FAILURE); };
while( (entry = readdir(dp)) != NULL ) {
if ( strcmp(entry->d_name, ".")
&& strcmp(entry->d_name, "..") ){
char buf[256];
if (snprintf(buf, sizeof(buf),
"/mnt/shared/%s", entry->d_name)
>=sizeof(buf))
{ fprintf(stderr, "too long path %s\n", buf);
exit(EXIT_FAILURE);
};
struct stat st;
if (stat(buf,&st)) {
perror(buf);
exit(EXIT_FAILURE);
};
if ((st.st_mode & S_IFMT) == S_IFREG // a plain file
&& (st.st_mtime < weekago))
{
if (remove(buf)) perror(buf);
}
}
}
My untested code above is imperfect (and not very well indented): it don't handle file paths wider than 255. But you could improve it, e.g. using asprintf(3) to build the path in heap (then you'll need to free it).
Practically speaking, use find(1). If you need to recurse in a file tree in C, use nftw(3)

PDCURSES assignign value to integer make error

I'm using PDCURSES on Windows 7 and my program stops working at the beginning.
I noticed the error is made by assigning value to integer in line: 41 .
Unfortunately I have no idea why.
I'm writing in C in Code blocks.
I would be grateful if someone could help me.
Error message
#include <curses.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define ILOSC_NAZW 5
#define false 0
#define true 1
WINDOW * createwin(int height,int width,int starty,int startx);
void uaktualnij(WINDOW*left,char *nazwa);
void wyswietl_nazwy(WINDOW *right,char **losowe_nazwy);
int poprawnie=0,blednie=0;
int main()
{
char *losowe_nazwy[ILOSC_NAZW]={"JACK","TOM","KEN","JESSY","ANDRIEJ"};
char str[20];
int c,startx,starty,height,width;
WINDOW *right,*left,*console;
initscr();
refresh();
startx=0,starty=0,height=LINES/2,width=COLS/2;
left=createwin(height,width,starty,startx);
startx=COLS/2;
right=createwin(height,width,starty,startx);
startx=0;
starty=LINES/2;
width=COLS;
console=createwin(height,width,starty,startx);
getmaxyx(right,starty,startx);
uaktualnij(right,NULL);
mvwprintw(left,starty/2-starty/4,startx/2-strlen("PODAJ NAZWE UZYTKOWNIKA")/2,"PODAJ NAZWE UZYTKOWNIKA");
wrefresh(left);
wyswietl_nazwy(left,losowe_nazwy);
c=0; // assigning value make error
getmaxyx(console,starty,startx);
wmove(console,starty-2,2);
memset(str,0,sizeof *str);
wclear(console);
wborder(console,0,0,0,0,0,0,0,0);
wrefresh(console);
getch();
endwin();
return 0;
}
WINDOW * createwin(int height,int width,int starty,int startx){
WINDOW *mywin=newwin(height,width,starty,startx);
box(mywin,0,0);
wrefresh(mywin);
return mywin;
}
void czekaj(double sec){
clock_t koniec=clock()+sec*CLOCKS_PER_SEC;
while(clock()<koniec)continue;
}
void wyswietl_nazwy(WINDOW *right,char **losowe_nazwy){
int i=ILOSC_NAZW;
int rstartx,rstarty;
getmaxyx(right,rstarty,rstartx);
while( i>=0)
{
mvwprintw(right,rstarty/2-rstarty/4+2,rstartx/2-strlen(losowe_nazwy[i])/2,"%s",losowe_nazwy[i]);
wrefresh(right);
czekaj(0.2);
wmove(right,rstarty/2-rstarty/4+2,rstartx/2-strlen(losowe_nazwy[i]));
wclrtoeol(right);
mvwaddch(right,rstarty/2-rstarty/4+2,rstartx-1,ACS_VLINE);
wrefresh(right);
i--;
}
}
void uaktualnij(WINDOW*left,char *nazwa){
int startx,starty;
wclear(left);
wborder(left,0,0,0,0,0,0,0,0);
getyx(left,starty,startx);
mvwprintw(left,starty+1,startx+1,"Poprawnie: %i",poprawnie);
mvwprintw(left,starty+2,startx+1,"Blednie: %i",blednie);
if(nazwa!=NULL)
mvwprintw(left,starty+3,startx+1,"Wproawadzona nazwa uzytkowanika: %s",nazwa);
wrefresh(left);
}
Since PDCurses is written in C, I am assuming that you are compiling in C.
Move the
int c;
to just below the line
char *losowe_nazwy[ILOSC_NAZW]={"JACK","TOM","KEN","JESSY","ANDRIEJ"};
int c;
The C language expects those sorts of definitions to be at the top of the function.

sys/wait.h and sys/kthread.h do not compile together

I am compiling a kernel module in linux related to creating kthreads to achieve parallelism but I am stuck at compiling issues.
Here is my code:
#include <linux/init.h>
#include <linux/errno.h>
#include <asm/byteorder.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/types.h>
#include <sys/wait.h>
void threadfn1()
{
int j;
for( j = 0; j < 1000000; j++ )
printk(KERN_INFO "I AM THREAD 1 %d\n",j);
}
void threadfn2()
{
int j;
for( j = 0; j < 1000000; j++ )
printk(KERN_INFO "I AM THREAD 2 %d\n",j);
}
static int __init abc_init(void)
{
struct task_struct *t1 = kthread_run(threadfn1, NULL, "thread1");
struct task_struct *t2 = kthread_run(threadfn2, NULL, "thread2");
printk(KERN_INFO "HELLO WORLD\n");
waitpid(-1,NULL,0); // whatever the parameters of waitpid() are
}
static void __exit abc_fini(void)
{
printk(KERN_INFO "BYE WORLD\n");
}
module_init(abc_init);
module_exit(abc_fini);
The problem with my code is that when i compile my kernel module with make, sys/wait.h gives compiling errors like "redefinition of some strcut xyz" any many more errors, when linux/module.h and linux/kthread.h are also included. As soon as i comment out these two files, the module compiles well but gives a linking error that "waitpid" is undefined.
Why doesnt sys/wait.h compile well with linux/kthread.h and linux/module.h? Has anyone encountered this problem before?
Any help would be appreciated.
It is incorrect to include userspace headers, like sys/wait.h, in kernel code.

Resources